0

以下のコードに示すように、ファイルに書き込むメソッドを作成しました。このメソッドは、指定された 2 つのパラメーターを使用して、別のメソッドから 1000 回呼び出されます。

ただし、これらの呼び出しのうちの 5 つで、このメソッドはエラー「e」をキャッチして出力します:しかし、ファイルにはこの結果がありません。(他の呼び出しは正常に機能します)

私は配列をまったく扱っていないので、最初は本当に混乱しました。少しグーグルで調べた後、これを見つけました: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

タイトなループでファイルを 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();
      }
    }
  }

これが機能しない場合は、VM のバグを回避できる可能性が低いので、FileOutputStream の代わりに FileWriter を使用してみてください。

于 2012-07-25T00:07:59.037 に答える
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 に答える