我有一个Writer程序,它将一行文本写入文件,然后等待用户在写入另一行之前返回,然后退出.只有在那之后文件才关闭.代码:
public class Writer {
Writer() {
}
public static String[] strings =
{
"Hello World",
"Goodbye World"
};
public static void main(String[] args)
throws java.io.IOException {
java.io.FileOutputStream pw =
new java.io.FileOutputStream("myfile.txt");
for(String s : strings) {
pw.write(s.getBytes());
System.in.read();
}
pw.close();
}
}
Run Code Online (Sandbox Code Playgroud)
首先开始:
java作家
然后我还有一个读者程序,只要文件的写入尚未完成(即尚未调用pw.close()),应该(我预期)阻止.代码:
public class ReaderFIS extends Object {
ReaderFIS() {
}
public static void main(String[] args) throws Exception {
java.io.FileInputStream in = new java.io.FileInputStream("myfile.txt");
int ch = -1;
while((ch = in.read()) >= 0) {
System.out.println("ch = …Run Code Online (Sandbox Code Playgroud)