简体   繁体   English

为什么无法在Java中读取我的文件?

[英]Why can my file not be read in java?

I am trying to simply read a text document in java. 我试图简单地读取Java中的文本文档。 The file is being found by my program; 我的程序正在找到该文件; I can see that it is able to determine the correct absolute path through various tests, but the problem seems to be that my program doesn't have permission to view the file? 我可以看到它能够通过各种测试确定正确的绝对路径,但是问题似乎出在我的程序没有查看文件的权限?

File names = new File("names.txt");
if(names.setReadable(true, false (edit: true)))
    System.out.println("Can now be read");
if(names.canRead())
    System.out.println("Can be read");

FileInputStream fs = new FileInputStream(names);
BufferedReader br = new BufferedReader(new InputStreamReader(fs));

The boolean expression in each if statement evaluates to false, and I cannot understand why, or how to change it. 每个if语句中的布尔表达式的计算结果均为false,我无法理解为什么或如何更改它。 I run into a FileNotFoundException where I attempt to create a new FileInputStream, which I read is due the the file being unreadable. 我遇到FileNotFoundException,在其中尝试创建一个新的FileInputStream,由于文件不可读,因此我读取了该文件。

EDIT: Now I have changed the second paramater for the setReadable method call to true, and that part works (it prints "Can now be read"); 编辑:现在,我已将setReadable方法调用的第二个参数更改为true,并且该部分正常工作(它打印“现在可以读取”); so it seems that the file is being found and set as readable but still the second if statement fails and the program cannot access the text document. 因此,似乎已找到文件并将其设置为可读文件,但仍然是第二个if语句失败,并且程序无法访问文本文档。

This should be: 应该是:

if(names.setReadable(true, true))

The second argument should be true so that the owner can access the file. 第二个参数应为true以便所有者可以访问文件。

If you set it to false the program won't be able to read the file. 如果将其设置为false,则程序将无法读取该文件。

For more information read here . 欲了解更多信息,请点击这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM