Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

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

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
    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
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

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

    Print out the value of configFiles.get(i) and verify the value is a valid file (including path if necessary)

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by jps View Post
    Print out the value of configFiles.get(i) and verify the value is a valid file (including path if necessary)
    I find out what the problem was. The problem is that the program is running in the wrong directory, ../bin instead of ../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 I don't know how this works and neither how I can fix it (run ir 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
    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:
    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:
    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
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 262 Times in 231 Posts

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

    So do you still have a question or is everything working now?

  5. #5
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    Quote Originally Posted by jps View Post
    So do you still have a question or is everything working now?
    What? Nothing is working now, it's worst than before. As I said, not only I can't run the program in the correct directory but I am also not being able to read the file contents

Similar Threads

  1. 'Can't find file' error
    By WayneC in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: May 14th, 2012, 01:30 AM
  2. Find the important class in ".mse" file using Java program
    By ashwarth21 in forum Java Theory & Questions
    Replies: 3
    Last Post: March 13th, 2012, 07:43 PM
  3. Can't find file (not sure the error)
    By KILL3RTACO in forum File I/O & Other I/O Streams
    Replies: 21
    Last Post: September 1st, 2011, 01:17 AM
  4. [SOLVED] If a file extension is known, but not the name, how do you find it?
    By techwiz24 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: August 3rd, 2011, 03:09 PM
  5. recursive program to find median
    By TeamRival in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 26th, 2011, 10:40 PM