-1

I have write the following code to write a file-

FileOutputStream fos = context.openFileOutput("MyFile",Context.MODE_PRIVATE);

ObjectOutputStream os = new ObjectOutputStream(fos);

//Saving my list
os.writeObject(EmailStackClass.list);
os.close();

how can i identify if the file is already exist ? Because if the file is created previously, then i have to append my data on the previous file. Please help me...

user2166895
  • 81
  • 1
  • 1
  • 7
  • 2
    Welcome to StackOverflow! Please, have a look around before making new questions... http://stackoverflow.com/questions/16237950/android-check-if-file-exists-without-creating-a-new-one – Seraphim's Apr 29 '14 at 09:59
  • http://stackoverflow.com/a/16238204/1665507 check it out. – Spring Breaker Apr 29 '14 at 09:59

3 Answers3

1

try below code :-

File f = new File("MyFile"); // PATH OF YOUR FILE
        if(f.exists())
        {
            System.out.println("file exist");
        }
        else
        {
            System.out.println("file not exist");
        }
duggu
  • 37,851
  • 12
  • 116
  • 113
0

You can check as follows...

File file = getBaseContext().getFileStreamPath(fileName);
if(file.exists()) {

    //do something with the existed file
}
Hamid Shatu
  • 9,664
  • 4
  • 30
  • 41
0

First Check this..

File file = new File("dirPath", "fileName");
if(file.exists()){
        // file exists
}else{
        // file not exists
}

hope this help you..

jignesh.world
  • 1,396
  • 1
  • 11
  • 26