0

Iam trying to add a path (string) over an editfield in class main, the path will be taken to another class extract. After this I want to read the file with FileReader, but I got some error: file not found.

So I does some test:

  1. I wrote the path directly in the FileReader -> everything Okay
  2. I wrote a function File named sFile to get the path from class main and try to find the file behinde the path (exists). The file could be found but if FileReader trying to load the file got the same error

Code:

    File sFile = new File(path);

    if (sFile.exists()){
        System.out.println("Found.");
        System.out.println(sFile.getAbsolutePath());
        try{
            FileReader file = new FileReader(sFile); //db10916358-hp.sql (test file)
            String[] fReadTmp = new String[10240000];//Just for testing

            BufferedReader br = new BufferedReader(file);
            String read = br.readLine();//Read a line
Xrade
  • 11
  • 1
  • 2
    Do you have sufficient rights to the file? Also there is no guarantee the file would still exist after you return from `exists`. – Neijwiert Dec 21 '15 at 12:50
  • 2
    Can we see the stacktrace? –  Dec 21 '15 at 12:50
  • 4
    Possible duplicate of [Java new File() says FileNotFoundException but file exists](http://stackoverflow.com/questions/19307622/java-new-file-says-filenotfoundexception-but-file-exists) – slim Dec 21 '15 at 12:58
  • I run the code and it works. Please show the stacktrace or provide more information: OS, IDE, Java version, etc. – Dmytro Plekhotkin Dec 21 '15 at 13:04
  • post the code of method u use to get the sFile please. – Robdll Dec 21 '15 at 13:08

2 Answers2

0

I found the error, it was an other File function which creates some files from the extract.

It was so simple, sorry for that.

Thanks for your time!

Xrade
  • 11
  • 1
0

Try this snippet of code its work correctly

    public static void readFile(String path) throws FileNotFoundException, IOException{


       File file = new File(path);
       if(file.exists())
       {

        FileReader fileReader = new FileReader(file); //db10916358-hp.sql (test file)

        BufferedReader br = new BufferedReader(fileReader);
        String read = br.readLine();//Read a line
       }
       else 
       {

       System.out.print("Not Found");
       }

}