使用File对象初始化FileInputStream时获取FileNotFoundException [英] Get FileNotFoundException when initialising FileInputStream with File object
问题描述
我正在尝试使用File对象初始化FileInputStream对象。我在行上收到FileNotFound错误
I am trying to initialise a FileInputStream object using a File object. I am getting a FileNotFound error on the line
fis = new FileInputStream(file);
这很奇怪,因为我通过相同的方法打开了这个文件多次执行正则表达式。
This is strange since I have opened this file through the same method to do regex many times.
我的方法如下:
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(访问被拒绝)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream。(Unknown Source)
at java.io.FileInputStream。(Unknown Source)
    在controller.ScanEditRegions.fileToBIS(ScanEditRegions.java:52)
结果    在controller.ScanEditRegions.tidyHTML(ScanEditRegions.java:38)
,点击    在controller.ScanEditRegions.process(ScanEditRegions.java:64)
结果    在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)
at controller.Manager.main(Manager.java:10)
推荐答案
根据您粘贴在帖子中的堆栈跟踪来判断我猜你没有权限读取文件。
Judging by the stacktrace you pasted in your post I'd guess that you do not have the rights to read the file.
File类允许你对文件执行有用的检查,其中一些:
The File class allows you to performs useful checks on a file, some of them:
boolean canExecute();
boolean canRead();
boolean canWrite();
boolean exists();
boolean isFile();
boolean isDirectory();
例如,您可以检查:exists()&& isFile()&& canRead()并打印更好的错误消息,具体取决于您无法读取文件的原因。
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.
这篇关于使用File对象初始化FileInputStream时获取FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!