We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

java file

vkchaitu82
vkchaitu82 asked
on
Medium Priority
330 Views
Last Modified: 2012-05-11
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.
Comment
Watch Question

Awarded 2011
Awarded 2011

Commented:
File f
if(f.exists()){..}
Awarded 2011
Awarded 2011

Commented:
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
...
Awarded 2011
Awarded 2011
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Awarded 2011
Awarded 2011

Commented:

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
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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);                                          
Awarded 2011
Awarded 2011

Commented:
It should be in quotes, I understand, :
filename= "test.txt";
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
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++)
{
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.