- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我从 ObjectInputStream
获取连续数据,我正在使用 BufferedWriter
对象将其写入文件。我正在检查文件大小不断增加。现在,一旦我停止用 false
停止 while(true)
方法,该方法继续运行该方法,我就会关闭我的 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
最佳答案
您需要关闭输出流。目前,您正在 catch
子句中执行此操作,如果您没有遇到 IOException
(并且您不希望出现这种情况),则不会到达该子句
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();
}
关于java - 关闭 BufferedWriter 会删除写入文件的所有文件数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36177757/
我正在使用一种简单的方法将字符串写入我的应用程序中的文件,如下所示: dateFormat = new SimpleDateFormat("dd MMMM YYYY HH:mm:ss"); cal =
File file = new File("result.csv"); FileWriter fw = new FileWriter(file.getAbsoluteFile()); Buffered
我必须从用户那里获取用户的姓名和地址并将其放入文本文件中。我写了以下代码: package selfTest.nameAndAddress; import com.intellij.codeInsig
我有以下示例应用程序,其中我写入文件,并在关闭编写器后立即尝试从文件中读取数据,但令我惊讶的是,没有任何内容写入文件。 我已经确保在读取文件之前调用了flush()和close()方法,但即使这样也对
我想将一些输入字符串设置为标准输入,以便另一个进程可以读取标准输入作为输入。我使用 Java BufferedWriter。但是,当输入字符串大于管道大小或其他进程无法读取时,如何处理这种情况?是否有
我有一种方法可以将 44 MB 的数据从 ResultSet 写入 CSV 文件。然而,完成大约需要 3.5 分钟。对于只有 44 MB 的数据来说,这似乎很慢。任何人都可以看到任何减慢我的代码速度的
我为 Android 4.1 或更高版本编写了一个小型演示应用程序,当单击按钮时,它应该使用 BufferedWriter 将字符串写入文件中,并且应该读取该字符串并将按钮的文本设置为此字符串。为了避
我有下面的代码片段。 Map globalMap = new HashMap(); File talendLogFile = new File("C:\\Softwares\\logFiles\\er
我有一个 BufferedWriter,它已经写入文件,但不是以我想要的方式。我在循环中初始化它,使其仅附加循环的最后一个结果以添加到文件中。实际上这对我来说没问题,但是文件中以前的结果被删除了。我希
这个问题已经有答案了: BufferedWriter not writing everything to its output file (8 个回答) 已关闭 8 年前。 所以我编写了一个程序,假设
我是否可以使用 BufferedWriter(或任何与此相关的编写器类)在无限循环的每次迭代期间将新行输出到文本文件,直到程序停止?据我了解,必须调用 BufferedWriter.close() 命
我想将一些字符串写入文件(使用 Java 8)。此代码片段编译时没有任何错误,但未写入文件....我是 noob ofcorz...有什么建议吗? import java.io.BufferedWri
我编写了一个方法来准备 SQL 插入语句并将它们存储在文本文件中。至此,它就完成了工作,但是我有一个完全碍眼的 catch block 。如果我能以某种方式关闭 BufferedReader 而无需隐
大家好,想知道是否可以帮助我,我正在尝试创建一个日志文件,其中保存用户选择的所有变量,到目前为止我已经得到: try{ String content = (" User : " +
我正在尝试重写这本字典:dictionary.txt按长度而不是按字母顺序排序。我有以下代码(在 main(String[] args) 内): BufferedReader read = n
我想读入一个文件并创建该文件的副本,但我的代码只写入文件中的最后一行。如何使每当我调用 write() 时,它都会写入新行。我想为副本创建一个新文件,因此无法将 true 添加到 FileWriter
已关闭。这个问题是 not reproducible or was caused by typos 。目前不接受答案。 这个问题是由拼写错误或无法再重现的问题引起的。虽然类似的问题可能是 on-top
我有一个 BufferedWriter ,用于写入刚刚在给定目录中创建的文件,但是,由于某种原因,它没有写入从另一个文件读取的文本,这是我的代码: private static final Strin
我必须从用户那里获取用户的姓名和地址并将其放入文本文件中。我写了以下代码: package selfTest.nameAndAddress; import com.intellij.codeInsig
我使用缓冲写入器将数组中的内容写入文本文件 try { File file = new File("Details.txt"); if (!file.exists()) {
我是一名优秀的程序员,十分优秀!