java.io.FileNotFoundException:(访问被拒绝)
我试图读取文件夹内的文件,但是当我运行该程序时,它会抛出此异常。我也尝试过其他一些文件夹。它抛出相同的异常。
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)
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您无法打开和读取目录,请使用
isFile()
和isDirectory()
方法来区分文件和文件夹。您可以使用list()
和listFiles()
方法获取文件夹的内容(分别用于文件名和File
),您也可以指定选择列出的文件子集的过滤器。You cannot open and read a directory, use the
isFile()
andisDirectory()
methods to distinguish between files and folders. You can get the contents of folders using thelist()
andlistFiles()
methods (for filenames andFile
s respectively) you can also specify a filter that selects a subset of files listed.此外,在某些情况下,检查目标文件夹权限也很重要。为用户授予写入权限可能是解决方案。这对我有用。
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.
这是我刚刚发现的一个问题 - 也许它可能对其他人有帮助。如果使用 Windows,则类文件夹不得启用加密! Tomcat似乎不喜欢这样。右键单击类文件夹,选择“属性”,然后单击“高级...”按钮。确保清除“加密内容以保护数据”复选框。重新启动 Tomcat。
它对我有用,所以希望它对其他人也有帮助。
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.
It worked for me so here's hoping it helps someone else, too.
正确检查文件路径,通常我们提到位置而忘记指定文件名或其所属的确切位置。
Check the file path properly, usually we mention the location and forget to specify the file name or the exact position where it belongs to.
这个解决方案有效:
我的JDK安装在一个目录(C盘)中并且
我的java程序以及其他文件都在另一个目录(D盘)中。
这是另一个 Stack Overflow 问题的解决方案
This solution worked:
My JDK is installed in one directory(C drive) and
my java program as well as other files are in another directory(D drive).
Here's the solution from another Stack Overflow question