Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test if file exists

Tags:

java

file

android

I'm trying to open a file in android like this :

  try    {       FileInputStream fIn = context.openFileInput(FILE);       DataInputStream in = new DataInputStream(fIn);       BufferedReader br = new BufferedReader(new InputStreamReader(in));       if(in!=null)           in.close();    }    catch(Exception e)    {  } 

, but in case the file does not exists a file not found exception is thrown . I'd like to know how could I test if the file exists before attempting to open it.

like image 718
rantravee Avatar asked May 07 '10 06:05

rantravee


1 Answers

I think the best way to know if a file exists, without actually trying to open it, is as follows:

File file = getContext().getFileStreamPath(FILE_NAME); if(file.exists()) ... 

Hope that helps, bye!

like image 149
lencinhaus Avatar answered Oct 01 '22 03:10

lencinhaus