简体   繁体   中英

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 . 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/ ). It returns true, so the file definitely does exist, and the path is right, however BufferedReader stil can't find it. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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