java.io.FileNotFoundException:(访问被拒绝)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4281143/
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
java.io.FileNotFoundException: (Access is denied)
提问by John
I am trying to read the files inside a folder, but when I run the program it throws this exception. I tried with some other folders also. It throws the same exception.
我正在尝试读取文件夹中的文件,但是当我运行该程序时,它会引发此异常。我也尝试过其他一些文件夹。它抛出相同的异常。
Exception in thread "main" java.io.FileNotFoundException: C:\backup (Access is denied)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
采纳答案by rsp
You cannot open and read a directory, use the isFile()
and isDirectory()
methods to distinguish between files and folders. You can get the contents of folders using the list()
and listFiles()
methods (for filenames and File
s respectively) you can also specify a filter that selects a subset of files listed.
您无法打开和读取目录,请使用isFile()
和isDirectory()
方法来区分文件和文件夹。您可以使用list()
和listFiles()
方法(File
分别用于文件名和s)获取文件夹的内容,您还可以指定一个过滤器来选择列出的文件子集。
回答by AlexR
- check the rsp's reply
- check that you have permissions to read the file
- check whether the file is not locked by other application. It is relevant mostly if you are on windows. for example I think that you can get the exception if you are trying to read the file while it is opened in notepad
- 检查 rsp 的回复
- 检查您是否具有读取文件的权限
- 检查文件是否没有被其他应用程序锁定。如果您在 Windows 上,这主要是相关的。例如,我认为如果您在记事本中打开文件时尝试读取该文件,则会出现异常
回答by Thales Valias
Also, in some cases is important to check the target folder permissions. To give write permission for the user might be the solution. That worked for me.
此外,在某些情况下,检查目标文件夹的权限也很重要。为用户授予写权限可能是解决方案。那对我有用。
回答by Rex the Strange
Here's a gotcha that I just discovered - perhaps it might help someone else. If using windows the classes folder must not have encryption enabled! Tomcat doesn't seem to like that. Right click on the classes folder, select "Properties" and then click the "Advanced..." button. Make sure the "Encrypt contents to secure data" checkbox is cleared. Restart Tomcat.
这是我刚刚发现的一个问题 - 也许它可能对其他人有所帮助。如果使用 Windows,则类文件夹不得启用加密!Tomcat 似乎不喜欢那样。右键单击类文件夹,选择“属性”,然后单击“高级...”按钮。确保清除“加密内容以保护数据”复选框。重启Tomcat。
It worked for me so here's hoping it helps someone else, too.
它对我有用,所以希望它也能帮助其他人。