Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect underlying cause for java.io.FileNotFoundException

FileNotFoundException is thrown on all sorts of occasions - not necessarily only when the file name is invalid, but also when e. g. permissions do not allow a file to be created or read:

java.io.FileNotFoundException: \\server\share\directory\test.csv (Anmeldung fehlgeschlagen: unbekannter Benutzername oder falsches Kennwort)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
    at java.io.FileWriter.<init>(FileWriter.java:73)

The above example shows a German Windows complaining about invalid username or password.

Is there a way short of parsing the exceptions message to get a little finer grained information on why exactly the exception occurred? Problem with message parsing is that in different locales the messages will vary.

like image 770
Daniel Schneller Avatar asked Nov 02 '09 12:11

Daniel Schneller


People also ask

How do I resolve Java IO FileNotFoundException?

How to Fix FileNotFoundException. Since FileNotFoundException is a checked exception, a try-catch block should be used to handle it. The try block should contain the lines of code that can throw the exception and the catch block should catch and handle the exception appropriately.

What is Java IO FileNotFoundException?

java.io.FileNotFoundException. Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream , FileOutputStream , and RandomAccessFile constructors when a file with the specified pathname does not exist.

In which cases FileNotFoundException at compile time will be thrown?

FileNotFoundException is a checked exception is used that occurs when a file path specified for accessing does not exist or is inaccessible. With the checked exception, it means that the java compiler checks at compile time if this exception has been handled or not; otherwise, a compile-time error occurs.

Is FileNotFoundException a runtime exception?

Now consider that FileNotFound is a runtime exception, the code hypothetically will look like below: FileInputStream fis = null; fis = new FileInputStream(new File("")); fis.


2 Answers

Do the check for file existence/read-write permissions yourself before creating FileOutputStream.

File test_csv = new File( "\\server\share\directory\test.csv" );

if ( test_csv.exists( ) && test_csv.canWrite( ) )
{
  // Create file writer
  ...
}
else
{
  // notify user
  ...
}

Notice that sometimes you will have to check the read/write permissions on a parent of you destination file, if you need to create a new file.

File test_csv = new File( "\\server\share\directory\test.csv" );
File parent_dir = test_csv.getParentFile( )

if ( parent_dir.exists( ) && parent_dir.canWrite( ) )
{
  // Create file writer
  ...
}
else
{
  // notify user
  ...
}
like image 64
Alexander Pogrebnyak Avatar answered Oct 03 '22 18:10

Alexander Pogrebnyak


You may want to look at the properties of the file using the java.io.File object before attempting to read the file. There's a canRead method on that you can use to determine whether or not the user can read the file.

like image 26
Chris Gow Avatar answered Oct 03 '22 17:10

Chris Gow