Karthick
Karthick

Reputation: 23

get file location in android?

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??

Upvotes: 1

Views: 680

Answers (2)

naikus
naikus

Reputation: 24472

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");
}

Upvotes: 2

Cristian
Cristian

Reputation: 200170

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");
}

Upvotes: 1

Related Questions