BufferedWriter
オブジェクトを使用して私のファイルに書き込んでいるコンティニュアデータfom が表示されています。私は増加し続けるファイルサイズを確認しています。さて、私はこのメソッドを実行し続けるfalse
でwhile(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
あなたが投稿していない場合は 'humanReadableByteCount'コードが...ファイルサイズを返すだけのコード –
そのを知ることは困難です。今更新された質問を確認してください –
コードはかなり疑わしいようです。そのようなパラメータとして 'Socket'を渡すと、それは良くありません。 – Kayaman