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

时间:2014-12-09 10:13:10

标签: java java-io

我试图将2个数组和一个来自不同方法的整数带入写入文件的方法中。我尝试引入使用参数,但它不起作用。希望你能帮忙

这就是我如何通过持有arry信息的方法传递参数。

write (NameX, ScoresArray, players); /

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

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++)
    {
        NameX[i] = inStream.readLine();
        ScoresArray[i] = inStream.readLine();

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

    inStream.close();



}

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

*************编辑的代码********************

    write(NameX,ScoresArray,players);
   reads();
    }


}//end of league table


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()抛出IOException {     BufferedReader inStream = new BufferedReader(new FileReader(&#34; Genius.txt&#34;));

// 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();

}

我仍然遇到同样的问题

3 个答案:

答案 0 :(得分:0)

IOException是经过检查的异常,您的函数write (NameX, ScoresArray, players);将被抛出IOException。 所以你应该在调用者中处理它。

try {
   write (NameX, ScoresArray, players); //
}catch(IOException ie){
  // do operation 
}

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

答案 1 :(得分:0)

好吧,我希望你正在寻找这个:

/*
 * 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());}

    }

}

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

答案 2 :(得分:0)

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

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();
}

然后使用:

try {
   write (NameX, ScoresArray, players); //
}catch(IOException io){
    //Handle an IO exception
}catch(Exception e){
    //Handle Exception
}

我担心我没有时间对此进行测试。