0

I cannot get to make the following code work:

public static void main(String[] args) {
    try {
        InputStream f = new FileInputStream("test.txt");
    } catch (Throwable t) {
        t.printStackTrace(System.err) ;
    }
}

This throws the exception that says that the file test.txt could not be found. I've tried putting it in pretty much every directory possible in the project, I've checked what the root directory was with:

System.out.println(new File(".").getAbsolutePath());

And no matter where I put the file, it will never find it. Even tried putting it in C:\test.txt and this does not work either

The returned exception is the following:

java.io.FileNotFoundException: test.txt (Le fichier spécifié est introuvable // means File not found)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at main.Main.main(Main.java:30)
Fatalize
  • 3,513
  • 15
  • 25
  • 2
    Well what does your `System.out.println` show? And what does the exception throw? The code you've shown really *will* work if you put the file in the right working directory... – Jon Skeet Jan 26 '14 at 16:17
  • Let's say your class file is in directory X. Where is the `test.txt` file, w.r.t. X? – Giulio Piancastelli Jan 26 '14 at 16:22
  • 1
    @GiulioPiancastelli: the location of the class doesn't matter at all. What matters is the current directory, i.e. the directory from which the `java` command is executed. – JB Nizet Jan 26 '14 at 16:23
  • You either need to specify absolute path, or relative path that relates to the root working directory. Otherwise FileReader will not find this file – Maciej Cygan Jan 26 '14 at 16:24
  • Maybe try going the other way, and listing the files (see, for example, http://stackoverflow.com/questions/2056221/recursively-list-files-in-java). This should tell you what java is seeing, and if the test.txt on the FS has any strangeness, like a space at the end of the filename or something. – Raman Jan 26 '14 at 16:31
  • @JBNizet so he should just put the file wherever the result of `new File(".").getAbsolutePath()` indicates, I guess? – Giulio Piancastelli Jan 26 '14 at 16:35
  • @GiulioPiancastelli: Yes, that's correct. – JB Nizet Jan 26 '14 at 16:37

1 Answers1

0

It depends from you starting path. If you run java from your home location java will fin the file on local dir.

Absolute path

put on c: and try

InputStream f = new FileInputStream("c:\\test.txt");

Relative path

put on local dir and test with the following information

String current = new java.io.File( "." ).getCanonicalPath();
System.out.println("Current dir:"+current);

File folder = new File(".");
File[] listOfFiles = folder.listFiles();

for (int i = 0; i < listOfFiles.length; i++) {
  if (listOfFiles[i].isFile()) {
    System.out.println("File :'" + listOfFiles[i].getName()."'");
  } else if (listOfFiles[i].isDirectory()) {
    System.out.println("Directory " + listOfFiles[i].getName());
  }
}
venergiac
  • 7,469
  • 2
  • 48
  • 70
  • 1
    Still does not find the file. And the code returns Current dir:C:\Users\Julien Cumin\Desktop\4IF\PLD_Reseau\NeuralNetwork\NeuralNetwork, which is perfectly normal – Fatalize Jan 26 '14 at 16:26
  • You suggest putting the file on `c:\\` but you confusingly ask for a directory listing of the working directory. – Raman Jan 26 '14 at 17:05
  • I think the problem is in the filename like test.txt.txt...but I'm not sure ... obviously FileInputStrem works! – venergiac Jan 26 '14 at 17:07