java.io.FileNotFoundException:(访问被拒绝)

发布于 2024-10-04 10:45:05 字数 285 浏览 7 评论 0原文

我试图读取文件夹内的文件,但是当我运行该程序时,它会抛出此异常。我也尝试过其他一些文件夹。它抛出相同的异常。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

假装不在乎 2024-10-11 10:45:05

您无法打开和读取目​​录,请使用 isFile()isDirectory() 方法来区分文件和文件夹。您可以使用 list()listFiles() 方法获取文件夹的内容(分别用于文件名和 File),您也可以指定选择列出的文件子集的过滤器。

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 Files respectively) you can also specify a filter that selects a subset of files listed.

九厘米的零° 2024-10-11 10:45:05
  1. 检查rsp的回复
  2. 检查您是否有读取该文件的权限
  3. 检查该文件是否未被其他应用程序锁定。如果你在 Windows 上,它是最相关的。例如,我认为如果您尝试在记事本中打开文件时读取该文件,您可能会遇到异常
  1. check the rsp's reply
  2. check that you have permissions to read the file
  3. 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
做个少女永远怀春 2024-10-11 10:45:05

此外,在某些情况下,检查目标文件夹权限也很重要。为用户授予写入权限可能是解决方案。这对我有用。

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.

嘦怹 2024-10-11 10:45:05

这是我刚刚发现的一个问题 - 也许它可能对其他人有帮助。如果使用 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.

内心激荡 2024-10-11 10:45:05

正确检查文件路径,通常我们提到位置而忘记指定文件名或其所属的确切位置。

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.

你在我安 2024-10-11 10:45:05

这个解决方案有效:

我的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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文