【发布时间】:2018-12-18 19:25:30
【问题描述】:
我正在尝试使用BufferedReader 来读取文件的行,但它给了我FileNotFoundException。经过一番搜索,我找到了一种检查文件是否存在的方法(我使用了这个:https://www.javabrahman.com/quick-tips/how-to-check-for-existence-of-a-file-in-java/)。它返回 true,因此文件确实存在,并且路径正确,但是 BufferedReader 仍然找不到它。我查看了一堆可能的解决方案,但似乎没有一个有效。
这是我的代码:
import java.io.*;
import java.lang.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class neki
{
public static void main(String[] args)
{
File file = new File("C:\\Users\\natan\\Desktop\\words.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String curr="AAAAAA";
int st=11184810;
String word;
boolean digit=false;
while(!curr.equals("ffffff"))
{
curr=Integer.toHexString(st);
for(int i=0;i<6;i++)
{
char c=curr.charAt(i);
if(Character.isDigit(c))
{
digit=true;
}
}
if(!digit)
{
for(int j=0;j<466545;j++)
{
word=br.readLine();
word=word.toLowerCase();
if(curr==word)
{
System.out.println(curr);
}
}
}
st++;
digit=false;
Path filePath_1= Paths.get("C:\\Users\\natan\\Desktop\\words.txt");
boolean fileExists_1= Files.exists(filePath_1);
System.out.println("File 'bleh' exists: "+fileExists_1);
}
}
}
【问题讨论】:
-
您是否尝试在将 file 的 exists() 方法传递给 FileInputStream 之前调用它?
-
我怀疑您使用 File 为一种方法获取文件,但使用 Paths 为另一种方法获取文件。不能说我对这两个都太熟悉了,但似乎它们的工作方式不太一样。
-
@obermillerk 不要认为这是给定的(它有点复杂),但它们是可以互换的。这应该不是问题。虽然,我也强烈建议使用 Paths-API 而不是 File-API。
标签: java