java 使用 File 对象初始化 FileInputStream 时获取 FileNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/999771/
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
Get FileNotFoundException when initialising FileInputStream with File object
提问by Ankur
I am trying to initialise a FileInputStream object using a File object. I am getting a FileNotFound error on the line
我正在尝试使用 File 对象初始化 FileInputStream 对象。我在线上收到 FileNotFound 错误
fis = new FileInputStream(file);
This is strange since I have opened this file through the same method to do regex many times.
这很奇怪,因为我已经通过相同的方法打开这个文件多次执行正则表达式。
My method is as follows:
我的方法如下:
private BufferedInputStream fileToBIS(File file){
FileInputStream fis = null;
BufferedInputStream bis =null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bis;
}
java.io.FileNotFoundException: C:\dev\server\tomcat6\webapps\sample-site (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at java.io.FileInputStream.(Unknown Source)
at controller.ScanEditRegions.fileToBIS(ScanEditRegions.java:52)
at controller.ScanEditRegions.tidyHTML(ScanEditRegions.java:38)
at controller.ScanEditRegions.process(ScanEditRegions.java:64)
at controller.ScanEditRegions.visitAllDirsAndFiles(ScanEditRegions.java:148)
at controller.Manager.main(Manager.java:10)
java.io.FileNotFoundException: C:\dev\server\tomcat6\webapps\sample-site (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.(Unknown Source)
at java .io.FileInputStream.(Unknown Source)
at controller.ScanEditRegions.fileToBIS(ScanEditRegions.java:52)
at controller.ScanEditRegions.tidyHTML(ScanEditRegions.java:38)
at controller.ScanEditRegions.process(ScanEditRegions.java:64)
at controller .ScanEditRegions.visitAllDirsAndFiles(ScanEditRegions.java:148)
在 controller.Manager.main(Manager.java:10)
回答by Philipp
Judging by the stacktrace you pasted in your post I'd guess that you do not have the rights to read the file.
根据您粘贴在帖子中的堆栈跟踪来判断,我猜您无权读取该文件。
The File class allows you to performs useful checks on a file, some of them:
File 类允许您对文件执行有用的检查,其中一些:
boolean canExecute();
boolean canRead();
boolean canWrite();
boolean exists();
boolean isFile();
boolean isDirectory();
For example, you could check for: exists() && isFile() && canRead() and print a better error-message depending on the reason why you cant read the file.
例如,您可以检查:exists() && isFile() && canRead() 并根据无法读取文件的原因打印更好的错误消息。
回答by Bryan Kyle
You might want to make sure that (in order of likely-hood):
您可能想要确保(按可能的顺序):
- The file exists.
- The file is not a directory.
- You or the Java process have permissions to open the file.
- Another process doesn't have a lock on the file (likely, as you would probably receive a standard IOException instead of FileNotFoundException)
- 该文件存在。
- 该文件不是目录。
- 您或 Java 进程有权打开该文件。
- 另一个进程没有锁定文件(很可能,因为您可能会收到标准 IOException 而不是 FileNotFoundException)
回答by kgiannakakis
This is has to do with file permissions settings in the OS. You've started the java process as a user who has no access rights to the specific directory.
这与操作系统中的文件权限设置有关。您已经以没有特定目录访问权限的用户身份启动了 java 进程。
回答by JAM
I think you are executing the statement from eclipse or any java IDE and target file is also present in IDE workspace. You are getting the error as Eclipse cant read the target file in the same workspace. You can run your code from command prompt. It should not through any exception.
我认为您正在从 eclipse 或任何 java IDE 执行语句,并且目标文件也存在于 IDE 工作区中。您收到错误,因为 Eclipse 无法读取同一工作区中的目标文件。您可以从命令提示符运行您的代码。它不应该通过任何异常。

