12

I just wanted to read a file line by line. This was meant to be simple, but i just can't get it right!

String fileName = "C:/Users/Diogo/Desktop/Krs_Grafo/Graph.txt";
FileReader file = new FileReader(fileName);
BufferedReader inputStream = new BufferedReader(file);
System.out.println(inputStream.readLine());

i keep getting the error:

Exception in thread "main" java.io.FileNotFoundException: C:\Users\Diogo\Desktop\Krs_Grafo\Graph.txt (O sistema não pode encontrar o arquivo especificado)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at java.io.FileReader.<init>(FileReader.java:41)
at krs_grafo.Krs_Grafo.main(Krs_Grafo.java:51)
Java Result: 1

The system cant find the file, but i'm sure as hell it is there! I'm using Netbeans 7.0 on a Windows 7.

Any suggestions?

AS SAID IN THE COMMENTS, it was searching for "Graph" and not "Graph.txt". This was from a previous execution where I tried without the extension. So, I edited it to be coherent. It still doesn't work.

Doug Porter
  • 7,721
  • 4
  • 40
  • 55
Rauter
  • 521
  • 2
  • 5
  • 16
  • Saving your time with Google Translate: *O sistema não pode encontrar o arquivo especificado* -> *The system can not find the file specified* in Portuguese – Tomasz Nurkiewicz Jul 01 '11 at 13:32
  • 5
    The error message seems to indicate that it tries to open the Graph file, and not the Graph.txt file. Are you sure you're executing the right code/class? – JB Nizet Jul 01 '11 at 13:33
  • The system is sure as hell it's not there. Double check, file name and path. – Giann Jul 01 '11 at 13:34
  • I'm pretty sure you're not executing the code you posted. Here's the message I get when executing it. It contains the full path of the file, with its extension : "Exception in thread "main" java.io.FileNotFoundException: C:\Users\Diogo\Desktop\Krs_Grafo\Graph.txt (Le chemin d’accès spécifié est introuvable)" – JB Nizet Jul 01 '11 at 14:01
  • You were right, i had a previous execution that was searching for "(...)/Graph" only. But the same does happen to the "(...)/Graph.txt". – Rauter Jul 01 '11 at 15:10
  • 1
    You might post the output of `cd` and `dir` in the given directory. Also, I assume you're running as user Diogo? – Michael Brewer-Davis Jul 01 '11 at 15:30
  • Aha, now that's the problem... the file was actually .txt.txt, apparently someone changed the file extensions in this PC. Post this as an answer, Michael and thanks you =) – Rauter Jul 01 '11 at 15:37
  • 4
    Ahh... the useful and handy "hide file extensions" ! – Luciano Jul 01 '11 at 15:51
  • 2
    One should think that "hide " should be turned off on a developer PC. It's the **first** thing I do whenever I start with a new Windows PC. Apart from that, as sad as it sounds: cmd.exe is your friend. – Ingo May 02 '13 at 22:23

4 Answers4

10

The problem here is that the file name was actually "Graph.txt.txt" wich I couldn't see because the extensions were hidden.

Thanks to user "Michael Brewer-Davis" who asked in the comments for "output of cd and dir in the given directory".

Also point out that either / and \\ work just fine.

Rauter
  • 521
  • 2
  • 5
  • 16
0

I had a similar problem with a java.io.FileNotFoundException. I had downloaded a project from an e-mail, unzipped and stored on my Desktop, NOT my workspace which caused the FileNotFoundException.

To get the right path, I copied down the exact path from what was shown when I imported the project. and this fixed the problem for me.

Thomas Esch
  • 149
  • 1
  • 5
0
  1. As JB Nizet points in a comment, the error message hints that the program tried to open a "Graph" file (not path and no extension), which is not compatible with the code you are showing us. Are you sure that that error message comes from running that code? Didi you try to debug it (step by step) ?

  2. Windows 7? Perhaps you'd prefer to set up a working directory in some "nice" directory, like C:\wk\ or something like that, so that you can rule out permission problems and have nicer-shorter paths.

  3. The suggestion of some answers about backlasshes is not relevant. Forward slashes work nice in Java in Windows. No need to worry about that.

leonbloy
  • 73,180
  • 20
  • 142
  • 190
0

You need to add the try catch block.

public static void main(String...args){
     String fileName = "C:/Users/DY.Liu/Desktop/Krs_Grafo/Graph.txt";
    try{
        FileReader file = new FileReader(fileName);
        BufferedReader inputStream = new BufferedReader(file);
        System.out.println(inputStream.readLine());
    } catch (FileNotFoundException e){
        e.printStackTrace();

    } catch (IOException e){

    }
}
Danyun Liu
  • 3,072
  • 24
  • 22
  • try and catch will handle the FileNotFoundException yet it doesn't solve his problem since the file exists and should open for read – Saher Ahwal Sep 16 '14 at 11:42