在 Java 中打开现有文件并关闭它。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9782706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening an existing file in Java and closing it.
提问by Siddharthan
Is it possible to open a file a in java append data and close numerous times. For example:
是否可以在java append data中打开文件a并关闭多次。例如:
//---- psuedocode
//class variable declaration
FileWriter writer1 = new FileWriter(filename);
fn1:
writer.append(data - title);
fn2:
while(incomingdata == true){
writer.append(data)
writer.flush();
writer.close()
}
The problem lies in the while loop. The file is closed and cant be opened again. Any one can help me in this?
问题在于while循环。文件已关闭,无法再次打开。任何人都可以帮助我吗?
采纳答案by Dawood ibn Kareem
The answers that advise against closing and re-opening the file each time are quite correct.
建议不要每次都关闭和重新打开文件的答案是非常正确的。
However, if you absolutely have to do this (and it's not clear to me that you do), then you can create a new FileWriter each time. Pass true as the second argument when you construct a FileWriter, to get one that appends to the file instead of replacing it. Like
但是,如果您绝对必须这样做(我不清楚您是否这样做),那么您可以每次都创建一个新的 FileWriter。当您构造 FileWriter 时,将 true 作为第二个参数传递,以获得附加到文件而不是替换它的文件。喜欢
FileWriter writer1 = new FileWriter(filename, true);
回答by Nikhil
I don't recommend trying to close your file and then reopening it again. Opening a file is an expensive operation and the fewer times you do this, the better it is for your code to run quickly.
我不建议尝试关闭您的文件然后再次重新打开它。打开文件是一项代价高昂的操作,执行此操作的次数越少,代码运行得越快。
Open it once, and close the file once you're done writing to it. This would be outside your loop.
打开一次,完成写入后关闭文件。这将在您的循环之外。
回答by npinti
Once that the file is closed you will need to re-open it. Calling writer.flush()
should flush the stream. So basically, you will then remove writer.close()
from within the while
loop. This will allow you to close the file once that you will have finished with it.
文件关闭后,您将需要重新打开它。调用writer.flush()
应该刷新流。所以基本上,你会然后取出writer.close()
从内while
环路。这将允许您在完成文件后关闭该文件。
So you have two options, either remove writer.close()
from within the while
loop or else create a new FileWriter
instance at the beginning of your loop.
所以你有两个选择,要么writer.close()
从while
循环中删除,要么在循环FileWriter
开始时创建一个新实例。
回答by Anantha Krishnan
Once a stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously-closed stream, however, has no effect.
关闭流后,进一步的 write() 或 flush() 调用将导致抛出 IOException。但是,关闭先前关闭的流没有任何效果。
while(incomingdata == true){
writer.write(data)
}
writer.close()
You don't need to flush each time. as calling close()
will first flush data before closing the stream.
不需要每次都冲。因为调用close()
将在关闭流之前首先刷新数据。
Updated for
更新为
The file that i created has to be saved. For which im supposed to close it so the timestamp is updated. Thats when the file is synced live.
必须保存我创建的文件。我应该关闭它以便更新时间戳。那就是文件实时同步的时候。
Use it like this
像这样使用它
while(incomingdata == true){
writer.append(data);
writer.flush();
}
writer.close();