程序终止后 FileOutputStream 仅写入文件的错误
Error where FileOutputStream only writes to file after the program has been terminated
我过去遇到过这个错误,但从未完全理解它。关闭 OutputStream 后,无论 java 文件的位置或它的调用方式如何,完全搞砸所有顺序 运行s 或尝试写入另一个文件,即使是不同的方法使用写入文件。出于这个原因,我避免关闭流,即使不这样做是一个可怕的习惯。在我的程序中,我创建了一个测试用例,它有一个 close 语句,它破坏了我以前的所有流,由于某种原因,它们只在程序终止后才写入文件。
我保持文件位置打开,它会在适当的时间将文本写入文本文件,但是 Windows 中的 "Preview" 面板没有检测到它(这曾经发生过)。请注意,在意外关闭流之前,这一切都运行良好。有没有办法重置流?我已经尝试在此过程中冲洗它,但仍然没有像之前那样 运行。
创建文件的方法如下:
protected void createFile(String fileName, String content) {
try {
String fileLoc = PATH + fileName + ".txt";
File f = new File(fileLoc);
if(!f.isFile())
f.createNewFile();
FileOutputStream outputStream = new FileOutputStream(fileLoc);
byte[] strToBytes = content.getBytes();
outputStream.write(strToBytes);
} catch (IOException e) {
e.printStackTrace();
return;
}
}
以及读取文件的方法:
protected String readFile(String fileName) {
try {
StringBuilder sb = new StringBuilder("");
String fileLoc = PATH + fileName + ".txt";
File f = new File(fileLoc);
if(!f.exists())
return "null";
Scanner s = new Scanner(f);
int c = 0;
while(s.hasNext()) {
String str = s.nextLine();
sb.append(str);
if(s.hasNext())
sb.append("\n");
}
return sb.toString();
} catch(Exception e) {
e.printStackTrace();
return "null";
}
}
如果需要,我很乐意回答任何澄清问题。感谢您的协助。
没有try-resource,你需要在最后一个子句中关闭以确保没有泄漏。或者,如果您需要更多 'in-time' 更新,请使用 Stream.flush()
。
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
outputStream.close();
}
您需要在流上调用 flush()
以将字节写入流。
您目前正在自己呼叫 write()
,如下所示:
FileOutputStream outputStream = new FileOutputStream(fileLoc);
outputStream.write(content.getBytes());
你要做的是:
FileOutputStream outputStream = new FileOutputStream(fileLoc);
outputStream.write(content.getBytes());
outputStream.flush();
来自 OutputStream
的 Javadoc (https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html#flush--)(其中 FileOutputStream
是一个 OutputStream
),这是它对 flush()
的描述:
Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.
更好的方法是在 finally
块中关闭流,这样无论您的代码总是尝试释放任何打开的资源,如下所示:
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(fileLoc);
outputStream.write(content.getBytes());
outputStream.flush();
} finally {
if (outputStream != null) {
outputStream.close();
}
}
或者使用自动资源管理,像这样:
try (FileOutputStream outputStream = new FileOutputStream(fileLoc)) {
outputStream.write(content.getBytes());
outputStream.flush();
}
我过去遇到过这个错误,但从未完全理解它。关闭 OutputStream 后,无论 java 文件的位置或它的调用方式如何,完全搞砸所有顺序 运行s 或尝试写入另一个文件,即使是不同的方法使用写入文件。出于这个原因,我避免关闭流,即使不这样做是一个可怕的习惯。在我的程序中,我创建了一个测试用例,它有一个 close 语句,它破坏了我以前的所有流,由于某种原因,它们只在程序终止后才写入文件。
我保持文件位置打开,它会在适当的时间将文本写入文本文件,但是 Windows 中的 "Preview" 面板没有检测到它(这曾经发生过)。请注意,在意外关闭流之前,这一切都运行良好。有没有办法重置流?我已经尝试在此过程中冲洗它,但仍然没有像之前那样 运行。
创建文件的方法如下:
protected void createFile(String fileName, String content) {
try {
String fileLoc = PATH + fileName + ".txt";
File f = new File(fileLoc);
if(!f.isFile())
f.createNewFile();
FileOutputStream outputStream = new FileOutputStream(fileLoc);
byte[] strToBytes = content.getBytes();
outputStream.write(strToBytes);
} catch (IOException e) {
e.printStackTrace();
return;
}
}
以及读取文件的方法:
protected String readFile(String fileName) {
try {
StringBuilder sb = new StringBuilder("");
String fileLoc = PATH + fileName + ".txt";
File f = new File(fileLoc);
if(!f.exists())
return "null";
Scanner s = new Scanner(f);
int c = 0;
while(s.hasNext()) {
String str = s.nextLine();
sb.append(str);
if(s.hasNext())
sb.append("\n");
}
return sb.toString();
} catch(Exception e) {
e.printStackTrace();
return "null";
}
}
如果需要,我很乐意回答任何澄清问题。感谢您的协助。
没有try-resource,你需要在最后一个子句中关闭以确保没有泄漏。或者,如果您需要更多 'in-time' 更新,请使用 Stream.flush()
。
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
outputStream.close();
}
您需要在流上调用 flush()
以将字节写入流。
您目前正在自己呼叫 write()
,如下所示:
FileOutputStream outputStream = new FileOutputStream(fileLoc);
outputStream.write(content.getBytes());
你要做的是:
FileOutputStream outputStream = new FileOutputStream(fileLoc);
outputStream.write(content.getBytes());
outputStream.flush();
来自 OutputStream
的 Javadoc (https://docs.oracle.com/javase/8/docs/api/java/io/OutputStream.html#flush--)(其中 FileOutputStream
是一个 OutputStream
),这是它对 flush()
的描述:
Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.
更好的方法是在 finally
块中关闭流,这样无论您的代码总是尝试释放任何打开的资源,如下所示:
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(fileLoc);
outputStream.write(content.getBytes());
outputStream.flush();
} finally {
if (outputStream != null) {
outputStream.close();
}
}
或者使用自动资源管理,像这样:
try (FileOutputStream outputStream = new FileOutputStream(fileLoc)) {
outputStream.write(content.getBytes());
outputStream.flush();
}