java file

I want to know how I check in java if a file is already created (or) not ?
==
Details.
I have the following program.
=====================
filename= test.txt
for (i=0;i<10;i++)
{
                  FileWriter fstream = new FileWriter(filename);                  
                        out = new BufferedWriter(fstream);                                           
......
                           out.close()

}
==
when the loop repeats again, I dont want the file text.txt to be recreated every time.
I want to check if the file is already created add text to this file else create a new one.

Is there a way in JAVA to check if the file exists or not?
if it there , append to the file?

I want the                   FileWriter fstream = new FileWriter(filename);                  
to be inside the loop and not outside the loop.
vkchaitu82Asked:
Who is Participating?
 
for_yanCommented:
If you want too open file for appending then
ther uis a special constructor of FileWritter:

http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileWriter.html
0
 
for_yanCommented:
File f
if(f.exists()){..}
0
 
for_yanCommented:
File f = new File(filename);
then
boolean method exists()
of class File will tell you if such file exists or not
so

if(f.exists()){
//do somting
}
else
...
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
objectsCommented:
filename= test.txt
File file = new File(filename);
if (file.exists()) {
for (i=0;i<10;i++)
{
        FileWriter fstream = new FileWriter(filename);                  
        out = new BufferedWriter(fstream);                                          
0
 
for_yanCommented:

FileWriter(String fileName, boolean append)
if append is true it will append to the end of the file,
otherwise if you just open it for writing
 it will overwrite, so you need to be cautious
in such situations
0
 
objectsCommented:
sorry that should be:


filename= test.txt
File file = new File(filename);
if (file.exists()) {
   for (i=0;i<10;i++)
   {
        FileWriter fstream = new FileWriter(file);                  
        out = new BufferedWriter(fstream);                                          
0
 
for_yanCommented:
It should be in quotes, I understand, :
filename= "test.txt";
0
 
objectsCommented:
if you want to append then you can change the code to the follwoing and don't need to check if it exists
By the look you also should be opening the file outside that loop


filename= "test.txt";
File file = new File(filename);
FileWriter fstream = new FileWriter(file, true);                  
out = new BufferedWriter(fstream);                                          
for (i=0;i<10;i++)
{
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.