4

how to check if file exists and open it?

if(file is found)
{
    FileInputStream file = new FileInputStream("file");
}
ratchet freak
  • 47,288
  • 5
  • 68
  • 106
user93200
  • 289
  • 2
  • 6
  • 15
  • possible duplicate of [How do I check if a file exists? (Java on Windows)](http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-java-on-windows) – anubhava Mar 27 '12 at 18:02
  • Use [File#exists](http://docs.oracle.com/javase/6/docs/api/java/io/File.html#exists%28%29). – Sahil Muthoo Mar 27 '12 at 18:00

3 Answers3

20

File.isFile will tell you that a file exists and is not a directory.

Note, that the file could be deleted between your check and your attempt to open it, and that method does not check that the current user has read permissions.

File f = new File("file");
if (f.isFile() && f.canRead()) {
  try {
    // Open the stream.
    FileInputStream in = new FileInputStream(f);
    // To read chars from it, use new InputStreamReader
    // and specify the encoding.
    try {
      // Do something with in.
    } finally {
      in.close();
    }
  } catch (IOException ex) {
    // Appropriate error handling here.
  }
}
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • The way you close the stream is incorrect. The in.close() should be in the finally of first try. I give you a minus point to remind. – Charles Wu Oct 16 '14 at 07:08
  • @Charles Wu, Nothing is lost by closing `in` before handling any exception. Also, the `in` variable cannot be initialized outside an `IOException` handler because the `FileInputStream` constructor throws `IOException` handler, so if I moved the `finally` outside, I would have to do silly tricks like initializing `in` to null and checking that in the `finally` which just complicates things, and even if I did want to put up with that unnecessary complexity, I would need a `try` around the whole thing anyway because `in.close()` can throw an `IOException`. – Mike Samuel Oct 16 '14 at 12:02
  • My apology for misunderstanding your code. Your use the instance of FileInputStream in the inner try statement can close `in`. But the propagating exception of `in.close()` may overwrite the exception when another method of `in` also throws exception. – Charles Wu Dec 04 '14 at 02:10
  • @CharlesWu, True. If you want to handle an IO exception due to failure to close differently from an IO exception due to failure to read or write, then you should nest a try/catch within the finally. – Mike Samuel Dec 04 '14 at 15:57
7

You need to create a File object first, then use its exists() method to check. That file object can then be passed into the FileInputStream constructor.

File file = new File("file");    
if (file.exists()) {
    FileInputStream fileInputStream = new FileInputStream(file);
}
Adam
  • 35,919
  • 9
  • 100
  • 137
1

You can find the exists method in the documentation:

File file = new File(yourPath);
if(file.exists())
    FileInputStream file = new FileInputStream(file);
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
talnicolas
  • 13,885
  • 7
  • 36
  • 56