检测 java.io.FileNotFoundException 的根本原因

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1661050/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:49:06  来源:igfitidea点击:

Detect underlying cause for java.io.FileNotFoundException

javalocalizationfile-permissionsfilenotfoundexception

提问by Daniel Schneller

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:

FileNotFoundException 在各种情况下都会抛出 - 不一定只是在文件名无效时,而且在例如权限不允许创建或读取文件时:

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.

上面的例子显示了一个德语 Windows 抱怨无效的用户名或密码。

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

有没有办法通过解析异常消息来获得关于异常发生的确切原因的更细粒度的信息?消息解析的问题在于,在不同的语言环境中,消息会有所不同。

采纳答案by Alexander Pogrebnyak

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

在创建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
  ...
}

回答by kdgregory

One approach is to look at the actual type of the exception: as you can see from the docs, there are a lot of subclasses that provide finer-grained information.

一种方法是查看异常的实际类型:正如您从docs 中看到的,有很多子类提供更细粒度的信息。

However, you probably won't get far with that. As with most checked exceptions, it's usually better to log/report the exception and ask the user for choices on how to correct it.

但是,您可能不会走得太远。与大多数已检查的异常一样,通常最好记录/报告异常并询问用户如何更正它的选择。

回答by Chris Gow

You may want to look at the properties of the file using the java.io.Fileobject 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.

在尝试读取文件之前,您可能希望使用java.io.File对象查看文件的属性。您可以使用 canRead 方法来确定用户是否可以读取文件。