tags:

views:

35

answers:

2

Hi to all.I use the following code to check file availability

    File f1=new File("/data/data/com.myfiledemo/files/settings.dat");
    if(f1.exists())
    textview.setText("File Exist");

If i use the following code it's not responding

   File f1=new File("settings.dat");
   if(f1.exists())
   tv.setText("File Exist");

Here com.myfiledemo is my application package . I simply create the file like this

   fileInputstream = openFileInput("settings.dat");  

why It's not responding for the second if condition.??Is it Wrong??

+1  A: 

If you create the file by using openFileInput, then this is the way to check whether the file exists or not:

FileInputStream input = null;
try{
    input = openFileInput("settings.dat");
}
catch(FileNotFoundException e){
    // the file does not exists
}

if( input != null ){
    tv.setText("File Exist");
}
Cristian
Thank you for your response
Karthick
+2  A: 

The second code snippet is not the correct way to use, If you insist on using a java.io.File object, it should be:

File f1=new File(context.getFilesDir(), "settings.dat");
if(f1.exists()) {
  tv.setText("File Exist");
}
naikus
Thank you for your response.
Karthick
@Karthik you are welcome :)
naikus