Java 读取 txt.file - 访问被拒绝?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7894032/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 21:52:34  来源:igfitidea点击:

Java reading txt.file - access denied?

javafilenotfoundexceptionreadfile

提问by Kristian

I have created a txtfile which I have saved in My Documentson my computer. I am trying to read the txtfile through FileReaderand BufferedReader. However, when I try to run the program I get the error message:

我创建了一个txt文件,并将其保存在计算机上的“我的文档”中。我正在尝试txt通过FileReader和读取文件BufferedReader。但是,当我尝试运行该程序时,我收到错误消息:

java.io.FileNotFoundException: <filelocation> (Access is denied)

Does anyone know what causes this, and how I might fix the problem? I have tried saving the document other places too, but I always get this message. I am sure the file path is correclty entered.

有谁知道这是什么原因造成的,我该如何解决这个问题?我也尝试将文档保存在其他地方,但我总是收到此消息。我确定输入的文件路径是正确的。

采纳答案by Ashwinee K Jha

One random guess is that you may be having multiple instance of your test program running so the file is locked by earlier instance of your program which is still running. You can check open consoles in the eclipse or you can restart eclipse.

一个随机猜测是,您可能正在运行多个测试程序实例,因此该文件被仍在运行的程序的早期实例锁定。您可以在 eclipse 中检查打开的控制台,也可以重新启动 eclipse。

回答by user207421

java.io.FileNotFoundException: C:\Users\Kristian\Documents (Access is denied)

java.io.FileNotFoundException: C:\Users\Kristian\Documents(访问被拒绝)

You are trying to read a directory, not a .txt file.

您正在尝试读取目录,而不是 .txt 文件。

回答by Naveen Babu

there could be 2 types of issue.

可能有两种类型的问题。

  1. your path is containing space in folder or file name. you can solve it by using the non-8dot3 file name by using the command dir /p /xin command prompt of that folder path.
  2. you can save the form in your project folder and use relative path ./txt.file. if you have the file under a path say <project folder>/input/txt.file, your relative path will be ./input/txt.file. If this doesn't work try googling the code for file reading and you will find out your mistake.
  1. 您的路径在文件夹或文件名中包含空格。您可以通过使用该文件dir /p /x夹路径的命令提示符中的命令使用非8dot3文件名来解决它。
  2. 您可以将表单保存在项目文件夹中并使用相对路径./txt.file。如果你有一个路径下的文件说<project folder>/input/txt.file,你的相对路径将是./input/txt.file. 如果这不起作用,请尝试使用谷歌搜索文件读取代码,您会发现自己的错误。

回答by user3621271

Could be that you have to change the security settings on your folder. Go into the directory, right click properties, select Security , advances and change permission so that all child dependencies inherit the security changes.

可能是您必须更改文件夹的安全设置。进入目录,右键单击属性,选择安全性,推进并更改权限,以便所有子依赖项都继承安全性更改。