0

我创建了一个写入文件的方法,如下面的代码所示。使用两个给定参数,从另一个方法调用此方法 1000 次。

然而,在其中 5 次调用中,此方法捕获错误“e”并打印出:java.lang.ArrayIndexOutOfBoundsException,在我的终端中,我得到 System.out.println(""+(double)C/T) 的结果,但在文件中,这个结果丢失了。(其他电话工作正常)

一开始我真的很困惑,因为我根本不使用数组。谷歌搜索了一下后,我发现了这个:http ://www-01.ibm.com/support/docview.wss?uid=swg1IZ87600

我仍然很困惑,不知道如何解决这个问题。有人帮忙吗?

public void myMethod(int C, int T) {   
    try {
      FileOutputStream fout = new FileOutputStream ("output.txt", true );
      PrintStream ps = new PrintStream(fout);    
      System.out.println(""+(double)C/T);
      ps.println((double)C/T+"");
      // close file
      fout.close();
    }
    catch(Exception e) {
        System.err.println(e);
    }   
}  
4

2 回答 2

1

在我的 Windows JVM 上完美地为我工作:

package cruft;

import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 * ArrayIndexOutOfBoundsProblem
 * @author Michael
 * @link http://stackoverflow.com/questions/11640904/java-lang-arrayoutofbounds-exception-when-writing-to-a-file
 * @since 7/24/12 7:58 PM
 */
public class ArrayIndexOutOfBoundsProblem {

    public static void main(String[] args) {
        ArrayIndexOutOfBoundsProblem aioob = new ArrayIndexOutOfBoundsProblem();

        int c = ((args.length > 0) ? Integer.valueOf(args[0]) : 10);
        int t = ((args.length > 1) ? Integer.valueOf(args[1]) : 2);
        aioob.myMethod(c, t);
    }

    public void myMethod(int C, int T){
        try{
            FileOutputStream fout = new FileOutputStream("output.txt", true );
            PrintStream ps = new PrintStream(fout);
            System.out.println(""+(double)C/T);
            ps.println((double)C/T+"");
            // close file
            fout.close();
        }
        catch(Exception e)
        {
            System.err.println(e);
        }
    }
}

我会这样写:符合 Java 编码标准并使用更清晰的名称:

package cruft;

import java.io.*;

/**
 * ArrayIndexOutOfBoundsProblem
 * @author Michael
 * @link http://stackoverflow.com/questions/11640904/java-lang-arrayoutofbounds-exception-when-writing-to-a-file
 * @since 7/24/12 7:58 PM
 */
public class ArrayIndexOutOfBoundsProblem {

    public static void main(String[] args) {
        ArrayIndexOutOfBoundsProblem aioob = new ArrayIndexOutOfBoundsProblem();

        int c = ((args.length > 0) ? Integer.valueOf(args[0]) : 10);
        int t = ((args.length > 1) ? Integer.valueOf(args[1]) : 2);
        aioob.printValues(c, t);
    }

    public void printValues(int c, int t){
        printValues(System.out, c, t);
    }

    public void printValues(String outputFilePath, int c, int t) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(outputFilePath);
            PrintStream ps = new PrintStream(fos);
            printValues(ps, c, t);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            close(fos);
        }
    }

    public void printValues(PrintStream ps, int c, int t) {
        ps.println(String.format("c: %10d t: %10d ratio c/t: %10.4f", c, t, (double) c/t));
    }

    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
于 2012-07-25T00:01:56.043 回答
1

在一个紧密的循环中打开和关闭一个文件 1000 次对我来说听起来很可疑,所以我建议您首先在循环外初始化 FileOutputStream,如下所示:

  private void myMethod() {

    PrintStream fos = null;

    try {
      fos = new PrintStream(new FileOutputStream("output.txt", true));

      for (int i = 0; i < 1000; i++) {
        // Calculate these appropriately...
        int c = 0;
        int t = 0;
        fos.println((double) c / t);
      }

    }
    catch (IOException e) {
      e.printStackTrace();
    }
    finally {
      if (fos != null) {
        fos.close();
      }
    }
  }

如果这不起作用,请尝试使用 FileWriter 而不是 FileOutputStream,因为它可能会解决您的 VM 错误。

于 2012-07-25T00:07:59.037 回答