0
PrintStream out;
try {
   out = new PrintStream(new FileOutputStream( "C:\\RandomWalkResultater.txt", true));
   System.setOut(out);

Cでtxtファイルを作成していません:ところで、これは次を使用するMacで正常に機能します:

PrintStream out;
try {
     out = new PrintStream(new FileOutputStream("/Volumes/DATA/Desktop/RandomWalkResultater.txt", true));
            System.setOut(out);
4

3 に答える 3

0

使用後にストリームを閉じることを忘れないでください。これは、ほぼ確実に行われるように、finally ブロックで行うのが最善です。また、out 変数を宣言するときは、最初に に設定して、使用する前に何かnullに設定したことをコンパイラが認識できるようにします。次のようにします。

  // declare your Stream variable and initialize to null
  PrintStream out = null; 
  try {
     // FILE_STREAM_PATH is a String constant to your output path
     out = new PrintStream(new FileOutputStream(FILE_STREAM_PATH, true));
     System.setOut(out);

     // use your Stream here

  } catch (FileNotFoundException e) {
     e.printStackTrace();
  } finally {
     // ***** close the Stream in the finally block *****
     if (out != null) {
        out.close();
     }
  }
于 2013-11-09T16:03:58.117 に答える