首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将参数引入java输入输出方法

将参数引入java输入输出方法
EN

Stack Overflow用户
提问于 2014-12-09 18:13:10
回答 3查看 181关注 0票数 0

我尝试将两个数组和一个整数从不同的方法引入到写入文件的方法中。我尝试使用参数引入,但不起作用。希望你能帮上忙

这就是我从保存数组信息的方法中传递参数的方式。

代码语言:javascript
复制
write (NameX, ScoresArray, players); /

这是使用传递的数组参数来创建文件的方法。

代码语言:javascript
复制
public static void write (String NameX [], int ScoresArray [], int players ) throws IOException 
{
    PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt"));


    outputStream.println(players);

    for (int i = 0; i < NameX.length; i++)
    {
            outputStream.println(NameX[i] + ScoresArray [i] );
    }

    outputStream.close();



}

这是读取文件的方法

代码语言:javascript
复制
public static void reads () throws IOException 
{
    BufferedReader inStream = new BufferedReader(new FileReader("Genius.txt"));

    // Read in first file entry as an integer - the number of names stored       
    int players = Integer.parseInt(inStream.readLine());
    System.out.println(players);

    // Create an array big enough
    String [] NameX = new String[players];
    String [] ScoresArray = new String[players];

    // Now read in the names
    for (int i = 0; i < NameX.length; i++)
    {
        NameX[i] = inStream.readLine();
        ScoresArray[i] = inStream.readLine();

        System.out.println(NameX[i]);
        System.out.println(ScoresArray[i]);
    }

    inStream.close();



}

请您告诉我哪里出错了,以及如何创建一个保存数组并稍后读取该文件的文件。

*编辑代码*

代码语言:javascript
复制
    write(NameX,ScoresArray,players);
   reads();
    }


}//end of league table


public static void write (String NameX [], int ScoresArray [], int players ) throws IOException 

{

代码语言:javascript
复制
PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt"));


outputStream.println(players);

for (int i = 0; i < NameX.length; i++)
{
        outputStream.println(NameX[i] +":"+ ScoresArray [i] );
}

outputStream.close();

} public static void reads ()抛出新{ BufferedReader inStream =新BufferedReader(新FileReader("Genius.txt"));

代码语言:javascript
复制
// Read in first file entry as an integer - the number of names stored       
int players = Integer.parseInt(inStream.readLine());
System.out.println(players);

// Create an array big enough
String [] NameX = new String[players];
String [] ScoresArray = new String[players];

// Now read in the names
for (int i = 0; i < NameX.length; i++)
{
    String str = inStream.readLine();
    String vals[] = str.split(":");
    System.out.println(vals[0]+"   "+vals[1]);
}

inStream.close();

}

我仍然遇到同样的问题

EN

回答 3

Stack Overflow用户

发布于 2014-12-09 18:25:36

IOException是一个已检查的异常,而您的函数write (NameX, ScoresArray, players);是抛出IOException。所以你应该在caller中处理它。

代码语言:javascript
复制
try {
   write (NameX, ScoresArray, players); //
}catch(IOException ie){
  // do operation 
}

有关详细信息,请参阅:Lesson: Exceptions

票数 0
EN

Stack Overflow用户

发布于 2014-12-09 18:31:12

好吧,我希望你能找到这个:

代码语言:javascript
复制
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package test;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 *
 * @author manoj.sharma
 */
public class Test {

    /**
     * @param args the command line arguments
     */

    public static void write (String NameX [], int ScoresArray [], int players ) throws IOException 
{
    PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt"));


    outputStream.println(players);

    for (int i = 0; i < NameX.length; i++)
    {
            outputStream.println(NameX[i] +":"+ ScoresArray [i] );
    }

    outputStream.close();



}
  public static void reads () throws IOException 
{
    BufferedReader inStream = new BufferedReader(new FileReader("Genius.txt"));

    // Read in first file entry as an integer - the number of names stored       
    int players = Integer.parseInt(inStream.readLine());
    System.out.println(players);

    // Create an array big enough
    String [] NameX = new String[players];
    String [] ScoresArray = new String[players];

    // Now read in the names
    for (int i = 0; i < NameX.length; i++)
    {
        String str = inStream.readLine();
        String vals[] = str.split(":");
        System.out.println(vals[0]+"   "+vals[1]);
    }

    inStream.close();



}  
    public static void main(String[] args) {
        String [] names={"manoj","kushal"};
        int [] scores = {10,20};
        int p = 2;
       try{
       write(names,scores,p);
       reads();
      } catch(IOException e){System.out.println(e.getMessage());}

    }

}

我希望你已经找到了解决方案。

票数 0
EN

Stack Overflow用户

发布于 2014-12-09 18:52:55

也许你可以尝试使用java.lang.Exception而不是IOException,使用Exception应该覆盖所有可能抛出的异常类型。

代码语言:javascript
复制
public static void write (String NameX [], int ScoresArray [], int players ) throws Exception
{
    PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt"));
    outputStream.println(players);

    for (int i = 0; i < NameX.length; i++){
        outputStream.println(NameX[i] + ScoresArray [i] );
    }

    outputStream.close();
}

然后使用:

代码语言:javascript
复制
try {
   write (NameX, ScoresArray, players); //
}catch(IOException io){
    //Handle an IO exception
}catch(Exception e){
    //Handle Exception
}

不过,我恐怕还没有时间来测试这一点。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27376364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档