#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    8
    Rep Power
    0

    Trying to read 3 .txt files sequentially with readLine() returns null


    As the title says, when I try to read three .txt sequentially, I am getting null printed on the screen instead of the content of these files. 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
    In the place of the nulls I expect the file contents to be printed

    Here is the code:
    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();
        }
      }
    The
    Code:
    System.out.println (configFiles.get(i));
    is to test if I am passing the correct strings to the FileReaders, so it doesn't try to pass strings that do not correspond to the .txt files names (and the names being passed, File1.txt, File2.txt and File3.txt are correct). I also checked the content of the files and they are there, so it is not trying to read empty files.

    What I find weirdest is that this similar code that I use to read the name of these files is working as expected:
    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();
      }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Feb 2004
    Location
    Mobile, Alabama
    Posts
    4,023
    Rep Power
    1285
    Take a look at this line.

    Code:
    while ((line = br.readLine()) != null);
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    8
    Rep Power
    0
    Originally Posted by bullet
    Take a look at this line.

    Code:
    while ((line = br.readLine()) != null);
    Oh dear........thanks

IMN logo majestic logo threadwatch logo seochat tools logo