java 检查文件是否存在java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1237235/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:46:19  来源:igfitidea点击:

check file exists java

javafilecore

提问by Stephen C

I want create text file but if the file already exists it should not create new file but should append the text to the content (at the end) of the existing file. How can I do it in Java?

我想创建文本文件,但如果文件已经存在,则不应创建新文件,而应将文本附加到现有文件的内容(在末尾)。我怎样才能在 Java 中做到这一点?

for every one second I'm reading data from inputstream when i stop reading and again i start reading data at that time i should write to same file if file already exist

每隔一秒,当我停止读取时,我从输入流中读取数据,然后我再次开始读取数据,如果文件已经存在,我应该写入同一个文件

does I have to check the condition:

我是否必须检查条件:

if(file.exists){

} else{
   new File();
}

What I have to do?

我必须做什么?

回答by Kirtan

You can use the following code to append to a file which already exists -

您可以使用以下代码附加到已存在的文件 -

try {
    BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
    out.write("data");
    out.close();
} catch (IOException e) { }

If the second argument in FileWriter'sconstructor is true, then bytes will be written to the end of the file rather than the beginning.

如果FileWriter构造函数中的第二个参数是true,则字节将被写入文件的末尾而不是开头。

Quoting Stephen's comment:

引用斯蒂芬的评论:

...passing trueas the 2nd argument causes the file to be created if it doesn't exist and to be opened for appending if it does exist.

...true作为第二个参数传递会导致文件不存在时创建,如果存在则打开以进行追加。

回答by Stephen C

do i have to check the condition if(file.exists){ }else{ new File(); }

我需要检查条件吗 if(file.exists){ }else{ new File(); }

No you don't have to do that: see other answers for the solution.

不,您不必这样做:请参阅解决方案的其他答案。

Actually, it would be a bad ideato do something like that, as it creates a potential race condition that might make your application occasionallydie ... or clobber a file!

实际上,做这样的事情是个坏主意,因为它会产生潜在的竞争条件,这可能会使您的应用程序偶尔死掉……或破坏文件!

Suppose that the operating system preempted your application immediately after the file.exists()call returns false, and gave control to some other application. Then suppose that the other application created the file. Now when your application is resumed by the operating system it will not realise that the file has been created, and try to create it itself. Depending on the circumstance, this might clobber the existing file, or it might cause this application to throw an IOException due to a file locking conflict.

假设操作系统在file.exists()调用返回后立即抢占您的应用程序false,并将控制权交给其他应用程序。然后假设另一个应用程序创建了该文件。现在,当您的应用程序被操作系统恢复时,它不会意识到该文件已被创建,而是尝试自己创建它。根据情况,这可能会破坏现有文件,或者由于文件锁定冲突而导致此应用程序抛出 IOException。

Incidentally, new File()does not actually cause any file system objects to be created. That only happens when you 'open' the file; e.g. by calling new FileOutputStream(file);

顺便说一下,new File()实际上不会导致创建任何文件系统对象。只有当您“打开”文件时才会发生这种情况;例如通过调用new FileOutputStream(file);

回答by Adamski

If you wish to append to the file if it already exists there's no need to check for its existence at all using exists(). You merely need to create a FileWriterwith the append flag set to true; e.g.

如果您希望在文件已经存在的情况下附加到文件,则根本不需要使用exists(). 您只需要创建一个FileWriter将 append 标志设置为 true 即可;例如

PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("foo.txt", true)));
pw.println("Hello, World");
pw.close();

This will create the file if it does not exist or else append to the end of it otherwise.

如果文件不存在,这将创建文件,否则将附加到文件末尾。