2012-07-24 97 views
0

我已经创建了一个写入文件的方法,如下面的代码所示。这种方法从另一种方法被称为1000次,具有两个给定的参数。java.lang.ArrayOutOfBounds写入文件时发生异常

然而,在这些调用中的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); 
    } 
} 
+1

如果它实际上是OutputStreamWriter的错误,那么在您自己的代码中没有“修复”。 此外,println过载,可以采取一倍,没有必要做+“”的东西。 – 2012-07-25 00:00:34

+0

请发出堆栈跟踪 – Bohemian 2012-07-25 00:00:39

+0

您使用IBM的jdk吗? – 2012-07-25 00:03:56

回答

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