[英]Why is directory name which contains dot(s) in the end is treated as a directory even if doesn't exists using File object in Java?
[英]In java BufferedReader doesn't find a file, even when file exists (and it's existance is confirmed using java)!
我正在尝试使用BufferedReader
来读取文件的各行,但这给了我FileNotFoundException
。 经过一番搜索后,我找到了一种检查文件是否存在的方法(我使用了以下方法: https : //www.javabrahman.com/quick-tips/how-to-check-for-existence-of-a-file-in- java / )。 它返回true,因此该文件肯定确实存在,并且路径正确,但是BufferedReader stil无法找到它。 我研究了很多可能的解决方案,但似乎没有一个可行。 这是我的代码:
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);
}
}
}
这些路径不同,一个路径的路径为“ test \\”,另一个路径的路径为“ test \\”。
new File("C:\\\\Users\\\\natan\\\\Desktop\\\\test\\\\words.txt");
Paths.get("C:\\\\Users\\\\natan\\\\Desktop\\\\words.txt");
使用常量保存在多个地方引用的静态值可以防止这种错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.