• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

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.
0
vkchaitu82
Asked:
vkchaitu82
  • 5
  • 3
2 Solutions
 
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
 
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now