【问题标题】:Bringing arguments into java input output method将参数带入java输入输出方法
【发布时间】:2014-12-09 10:13:10
【问题描述】:

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

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

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

}

我仍然遇到同样的问题

【问题讨论】:

  • 您面临什么问题,您是否遇到任何异常......等等。
  • 它说未报告的 IOException:必须捕获或显示才能抛出。错误是针对我调用 write 方法并传递数组的行。
  • 实际上,您正在处理我发生检查异常的文件。 Java doc 说您必须为已检查的异常提供异常处理。所以把 try-catch 块放在你初始化类并对文件执行读/写操作的地方,然后检查输出,让我知道进一步的问题。

标签: java java-io


【解决方案1】:

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

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

更多详情请见:Lesson: Exceptions

【讨论】:

  • 这就是它的本意吗?
  • 是这个样子吗?public static void write () try { write (NameX, ScoresArray, player); // } catch(IOException ie) { PrintWriter outputStream = new PrintWriter(new FileWriter("Genius.txt")); outputStream.println(玩家); for (int i = 0; i
  • @jeeps95 不,//做操作意味着处理异常。不要把你的代码放在 catch 里面。
【解决方案2】:

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

/*
 * 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 manoj 10 kushal 20。没有任何异常错误消息。
【解决方案3】:

也许你可以尝试使用 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
}

恐怕我还没来得及测试这个。

【讨论】:

    猜你喜欢
    • 2013-06-21
    • 2016-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多