Check the existence of Java in the file

I want to create a text file, but if the file already exists, I should not create a new file, but attach the text to the contents of the existing file (last) How can I do it in Java?

Whenever I stop reading, I am reading data from the input stream, and when I start reading data again, if the file already exists, I should write to the same file

I must check the following conditions:

if(file.exists){

} else{
   new File();
}

What should I do?

Solution

You can attach to an existing file using the following code –

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

If the second parameter in the filewriter's constructor is true, bytes are written to the end of the file instead of the beginning

Quote Stephen's comments:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>