简体   繁体   English

在Java中,即使文件存在(并且已使用java确认存在),BufferedReader也找不到文件!

[英]In java BufferedReader doesn't find a file, even when file exists (and it's existance is confirmed using java)!

I am trying to use BufferedReader to read through the lines of a file, but it is giving me a FileNotFoundException . 我正在尝试使用BufferedReader来读取文件的各行,但这给了我FileNotFoundException After some searching i found a way to check if a file exists (i used this : https://www.javabrahman.com/quick-tips/how-to-check-for-existence-of-a-file-in-java/ ). 经过一番搜索后,我找到了一种检查文件是否存在的方法(我使用了以下方法: https : //www.javabrahman.com/quick-tips/how-to-check-for-existence-of-a-file-in- java / )。 It returns true, so the file definitely does exist, and the path is right, however BufferedReader stil can't find it. 它返回true,因此该文件肯定确实存在,并且路径正确,但是BufferedReader stil无法找到它。 I've looked at a bunch of possible solutions, but none seem to work. 我研究了很多可能的解决方案,但似乎没有一个可行。 Here is my code: 这是我的代码:

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);

        }
    }
} 

These paths are not the same, one has 'test\\' in the path, and the other doesn't. 这些路径不同,一个路径的路径为“ test \\”,另一个路径的路径为“ test \\”。

new File("C:\\\\Users\\\\natan\\\\Desktop\\\\test\\\\words.txt"); Paths.get("C:\\\\Users\\\\natan\\\\Desktop\\\\words.txt");

Using a constant to hold static values referenced in multiple places can prevent this kind of error. 使用常量保存在多个地方引用的静态值可以防止这种错误。

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

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