Results 1 to 9 of 9
  1. #1
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default My program can't find file, even it being there

    I am developing a program where I read a file whose content is the name of other files that I need to read their content.
    I do it and store every line (which represents a file name) in a list of strings.
    Then I create a method whose purpose it is print on screen the content of every file, line by line. Here the problem shows up. It starts to execute and then it says that the file whose name is stored in the list of strings cannot be found! Here is the output:
    ./java -classpath Procurador\ de\ Texto/ Main nomesArquivos.txt
    File1.txt
    File2.txt
    File3.txt
    Exception in thread "main" java.io.FileNotFoundException: File1.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:138)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:97)
    at java.io.FileReader.<init>(FileReader.java:58)
    at FileManager.readFile(FileManager.java:22)
    at Main.main(Main.java:9)
    And here is the code of the method
    Java Code:
    public void readFile () throws Exception {
        for (int i=0; i<configFiles.size(); i++) {
          FileReader fr = new FileReader (configFiles.get(i));
          BufferedReader br = new BufferedReader (fr); 
          while ((line = br.readLine()) != null); {
            System.out.println (line);
            //System.out.println ("Boto");   
          }
          fr.close();
        }
      }
    I know that the name of the files stored in the list (configFiles) are the correct names because I printed them in the screen and they are correct (As can be seen in the beginning of the quote)
    The name of the files itself are also correct.

    So what can be the problem?

    Thanks for any help.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: My program can't find file, even it being there

    Your program does not run in a directory where you think it runs; do a System.out.println(System.getProperty("user.dir")) ; and see for yourself.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default Re: My program can't find file, even it being there

    Quote Originally Posted by JosAH View Post
    Your program does not run in a directory where you think it runs; do a System.out.println(System.getProperty("user.dir")) ; and see for yourself.

    kind regards,

    Jos
    True. It is running in /media/windows/Arquivos de programas/jdk1.7.0_21/bin but should be running in /media/windows/Arquivos de programas/jdk1.7.0_21/bin/Procurador de Texto

    And I don't get how it is happening, because there is no Main.class in .../bin only in .../Procurador de texto.

    So how this works and how I can fix it (run in ../Procurador de Texto?)

    While I don't solve it I putted the files in the bin directory and continued to work on it.
    And now I am getting another problem. The contents of the text files that should be printed in the screen are not being captured by br.readLine.

    Here is the code
    Java Code:
    public void readFile () throws Exception {
        for (int i=0; i<configFiles.size(); i++) {
          System.out.println (configFiles.get(i));
          FileReader fr = new FileReader (configFiles.get(i));
          BufferedReader br = new BufferedReader (fr); 
          while ((line = br.readLine()) != null); 
            System.out.println (line);
          fr.close();
        }
      }
    and here is the output
    File1.txt
    File2.txt
    File3.txt
    /media/windows/Arquivos de programas/jdk1.7.0_21/bin
    File1.txt
    null
    File2.txt
    null
    File3.txt
    null
    I expected the file file contents being printed in the place of the three nulls. This code:
    Java Code:
    The System.out.println (configFiles.get(i));
    prints the name of the files that shall be opened to read, and as can be seen in the quote, they are correct (File1.txt, FIle2.txt and File3.txt before the nulls)
    The code is similar to this one:
    Java Code:
    public void readConfigFile (String input) throws Exception {
        FileReader fr = new FileReader (input); 
        BufferedReader br = new BufferedReader (fr);
        while ((line = br.readLine()) != null) 
          configFiles.add (line);
        fr.close();
      }
    where I read the configurationFile where the name of the files that I should read are.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: My program can't find file, even it being there

    Quote Originally Posted by Daniel Silvester View Post
    True. It is running in /media/windows/Arquivos de programas/jdk1.7.0_21/bin but should be running in /media/windows/Arquivos de programas/jdk1.7.0_21/bin/Procurador de Texto
    No it shouldn't; it runs where the JVM was started; but nothing forbids to set the "user.dir" property to the directory you want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default Re: My program can't find file, even it being there

    Quote Originally Posted by JosAH View Post
    No it shouldn't; it runs where the JVM was started; but nothing forbids to set the "user.dir" property to the directory you want.

    kind regards,

    Jos
    So, even if the executable file is in another directory, it is running the executable in the JVM directory and so It expects the text files to also be on the JVM directory? That it is what is happening?

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: My program can't find file, even it being there

    Quote Originally Posted by Daniel Silvester View Post
    So, even if the executable file is in another directory, it is running the executable in the JVM directory and so It expects the text files to also be on the JVM directory? That it is what is happening?
    Those are two issues:

    1) your program runs where the java executable file is stored; nothing you can do about it;
    2) if you don't give a path with the file name, i.e. /media/windows/Arquivos/File1.txt, your program assumes that File1.txt is stored in the current directory (just as all programs and commands do). You can either specify the path to the file name or set the "user.dir" property to a suitable value to change the current working directory (same as for a shell or cmd.exe)

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Join Date
    Dec 2009
    Posts
    18
    Rep Power
    0

    Default Re: My program can't find file, even it being there

    Quote Originally Posted by JosAH View Post
    Those are two issues:

    1) your program runs where the java executable file is stored; nothing you can do about it;
    2) if you don't give a path with the file name, i.e. /media/windows/Arquivos/File1.txt, your program assumes that File1.txt is stored in the current directory (just as all programs and commands do). You can either specify the path to the file name or set the "user.dir" property to a suitable value to change the current working directory (same as for a shell or cmd.exe)

    kind regards,

    Jos
    Thanks, Has any idea about what can be happening in the read of the files?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: My program can't find file, even it being there

    I think you're confusing matters by sticking your application under the JDK bin directory.
    That's for starters.
    As for the rest of it, essentially what Jos says.
    The execution directory is based on your location (not the location of the java.exe, by the way, it just looks like that because you've stuck your app in a silly place).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,732
    Blog Entries
    7
    Rep Power
    21

    Default Re: My program can't find file, even it being there

    Quote Originally Posted by Tolls View Post
    The execution directory is based on your location (not the location of the java.exe, by the way, it just looks like that because you've stuck your app in a silly place).
    Yep, I wrote it correct in reply #4 but I must've been posessed when I wrote it incorrectly in reply #6; The java.exe process inherits the cwd from its parent process (e.g. the shell/cmd.exe).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. java file reader, jgrasp can't find the file
    By aramiky818 in forum New To Java
    Replies: 3
    Last Post: 04-22-2011, 03:06 AM
  2. Replies: 3
    Last Post: 11-11-2010, 09:12 AM
  3. Replies: 1
    Last Post: 02-05-2010, 05:53 PM
  4. Replies: 0
    Last Post: 02-05-2010, 04:49 PM
  5. Replies: 1
    Last Post: 07-03-2009, 09:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •