2016-03-23 4 views
0

BufferedWriterオブジェクトを使用して私のファイルに書き込んでいるコンティニュアデータfom が表示されています。私は増加し続けるファイルサイズを確認しています。さて、私はこのメソッドを実行し続けるfalsewhile(true)メソッドを中止するとすぐに私のBufferedWriterオブジェクトを閉じています。この操作の後、ファイルに書き込まれたデータはすべて失われます。BufferedWriterを閉じると、ファイル上のすべてのファイルデータが消去されますか?

private void appendFile(JLabel jLab0x28, Socket client) 
    { 
     try 
     { 
      if(!continueMe) 
      { 
       fileOut.close(); 
       fileOut = null; 
       in.close(); 
       System.gc(); 
       jLab0x28.setText("<html> <font color='red'> Socket is closed "+client.isClosed()+" </font> </html>"); 
       System.out.println("File size after discontinuing "+ 
         humanReadableByteCount(new File("C:\\ISSUE124_Resolved.txt").length(), true)); 
      } 

      if(!client.isClosed()) 
      { 
       try 
       { 
        String data = in.readObject().toString(); 
        System.out.println("File size is "+ 
          humanReadableByteCount(new File("C:\\ISSUE124_Resolved.txt").length(), true)); 
        fileOut.append(data); 
        fileOut.flush(); 
       } 
       catch (EOFException exp) 
       { 
        continueMe = true; 
        exp.printStackTrace(); 
        System.out.println("A Stream has finished "+exp.toString()+"\n"); 
       } 
       catch (ClassNotFoundException exp) 
       { 
        exp.printStackTrace(); 
        System.err.println(exp.toString()); 
        continueMe = false; 
       } 
      } 
     } 
     catch(IOException exp) 
     { 
      try 
      { 
       fileOut.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      exp.printStackTrace(); 
      System.err.println("Exception "+exp.toString()); 
      jLab0x28.setText(exp.getMessage()); 
      continueMe = false; 
     } 
    } 
public static String humanReadableByteCount(long bytes, boolean si) { 
     int unit = si ? 1000 : 1024; 
     if (bytes < unit) return bytes + " B"; 
     int exp = (int) (Math.log(bytes)/Math.log(unit)); 
     String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); 
     return String.format("%.1f %sB", bytes/Math.pow(unit, exp), pre); 
    } 

ログデータ:

File size is 118.3 kB 
File size is 118.4 kB 
File size is 118.5 kB 
File size is 118.7 kB 
File size is 118.8 kB 
File size is 118.9 kB 
File size is 119.1 kB 
File size is 119.2 kB 
File size is 119.3 kB 
Done 
Closed Socket connection from client 
File size after discontinuing 0 B 
+1

あなたが投稿していない場合は 'humanReadableByteCount'コードが...ファイルサイズを返すだけのコード –

+0

そのを知ることは困難です。今更新された質問を確認してください –

+0

コードはかなり疑わしいようです。そのようなパラメータとして 'Socket'を渡すと、それは良くありません。 – Kayaman

答えて

1

あなたはOutputStreamを閉じる必要があります。現在、あなたは大丈夫です、ここで私が持っているあなたは何IOExceptionに遭遇していない(そしてあなたはこれを期待していない)場合には達していないcatch

catch(IOException exp) 
     { 
      try 
      { 
       fileOut.close(); 
      } 
    ... 

にこの

catch(IOException exp) 
     { 

      exp.printStackTrace(); 
      System.err.println("Exception "+exp.toString()); 
      jLab0x28.setText(exp.getMessage()); 
      continueMe = false; 
     } 
try 
{ 
    fileOut.close(); 
} 
catch (IOException e) 
{ 
     e.printStackTrace(); 
    } 
+0

はい、それは私がfileOut.write(data)のようなデータを書き込んだ直後と同じくらい良いでしょう。 fileOut.flush(); fileOut.close();ループが続くとすぐに私は例外に繋がるでしょう。私はFileWriterを使用して解決しましたが、まだ理解しています。 –

+0

ループの後に 'fileOut.close()'を置くと、もはや書き込みをしないか、またはすべてのループ反復で開くことと閉じることができるappendを使用しているので、それは効率が悪いです –

+0

私はそれをしました。 io.IOException:ストリームが閉じた –

0

のようなあなたのコードを変更することをやっていますBufferedWriterのFileWriterオブジェクトintseadを使用しましたが、ファイルデータは消去されません。すべてのコードは、私のためにトリックをしたObjectの変更だけです。

public static String humanReadableByteCount(long bytes, boolean si) { 
     int unit = si ? 1000 : 1024; 
     if (bytes < unit) return bytes + " B"; 
     int exp = (int) (Math.log(bytes)/Math.log(unit)); 
     String pre = (si ? "kMGTPE" : "KMGTPE").charAt(exp-1) + (si ? "" : "i"); 
     return String.format("%.1f %sB", bytes/Math.pow(unit, exp), pre); 
    } 

    private void appendFile(JLabel jLab0x28, Socket client) 
    { 
     try 
     { 
      if(!continueMe) 
      { 
       //fileOut.close(); 
       //fileOut = null; 
       fw.close(); 
       fw = null; 
       in.close(); 
       System.gc(); 
       jLab0x28.setText("<html> <font color='red'> Socket is closed "+client.isClosed()+" </font> </html>"); 
       System.out.println("File size after discontinuing "+ 
         humanReadableByteCount(new File("C:\\ISSUE124_Resolved.txt").length(), true)); 
      } 

      if(!client.isClosed()) 
      { 
       try 
       { 
        String data = in.readObject().toString(); 
        System.out.println("File size is "+ 
          humanReadableByteCount(new File("C:\\ISSUE124_Resolved.txt").length(), true)); 
        fw.write(data); 
        fw.flush(); 
        //fileOut.write(data); 
        //fileOut.flush(); 

       } 
       catch (EOFException exp) 
       { 
        continueMe = true; 
        exp.printStackTrace(); 
        System.out.println("A Stream has finished "+exp.toString()+"\n"); 
       } 
       catch (ClassNotFoundException exp) 
       { 
        exp.printStackTrace(); 
        System.err.println(exp.toString()); 
        continueMe = false; 
       } 
      } 
     } 
     catch(IOException exp) 
     { 
      try 
      { 
       //fileOut.close(); 
       fw.close(); 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      exp.printStackTrace(); 
      System.err.println("Exception "+exp.toString()); 
      jLab0x28.setText(exp.getMessage()); 
      continueMe = false; 
     } 
    } 


File size is 1.8 MB 
File size is 1.8 MB 
File size is 1.8 MB 
Done 
Closed Socket connection from client 
File size after discontinuing 1.8 MB 
関連する問題