2012-07-24 95 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錯誤。