0

My problem is that I want to iterate all the text files in a directory using Java but whenever I start for loop over files, it always results in half processing, like for example:- I have 80 files and I want to read content of each file then I start my loop, it executes only last half part of file. Note: I have tried FileInputStream, Scanner class, BufferedReader as well. In FileInputStream, it gives EOFException and half-iteration results for other two methods. Here is my code -

    package InputFiles;

    import java.io.*;
    import java.util.Scanner;

    public class FileInput {
    public static void main(String[] args)  {
    File folder=new File("C:\\Users\\erpra\\Downloads\\SomePath");
    File[] listOfFiles=folder.listFiles();
    BufferedReader br=null;
    try {
        for(File tempFile:listOfFiles){
            System.out.println(tempFile.getName());
            br=new BufferedReader(new FileReader(tempFile));
            String str;
            while ((str=br.readLine())!=null){
                System.out.println(str);
            }
        }

    }catch (IOException e){
        e.printStackTrace();
    }finally {
        try {
            br.close();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
   }
    }

1 Answers1

0

Your code seems to work for me but I would suggest to manage the FileReader and BufferedReader differently:

public static void main(String[] args) {
    File folder=new File("C:\\Users\\erpra\\Downloads\\SomePath");
    File[] listOfFiles=folder.listFiles();
    try {
        for (File tempFile : listOfFiles) {
            System.out.println(tempFile.getName());
            try (FileReader fr = new FileReader(tempFile); 
                 BufferedReader br = new BufferedReader(fr);) 
            {
                String str;
                while ((str = br.readLine()) != null) {
                    System.out.println(str);
                }
            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

By using try-with-resources every FileReader and BufferedReader will be closed properly at the end of the try block. You only close the last reader.

But as you can see I did not change the while loop as it should work fine this way.

Conffusion
  • 4,335
  • 2
  • 16
  • 28
  • Thanks for this solution but I again have same issue. I don't know why it is executing last files and ignoring files about top 40 files. – Sneh Raghav Sep 23 '20 at 14:05
  • how do you verify, maybe your output buffer (screen console) doesn't show eveything. What if you only print the filenames, not their content. Does it print all files ? – Conffusion Sep 23 '20 at 15:59
  • My console displays half of the content by skipping the first 40 files and then executes the later files. And yes, it prints the filenames as well but in order to retrieve the inner content of all the text files, it fails. Is this happening due to shortage of BufferReader capacity? I could show you the output display but it's kinda confidential. – Sneh Raghav Sep 23 '20 at 16:26
  • Normally the size of the file should not matter for a *Reader as it works more like a cursor going through the content. Don't need to see the content of the files but can you give an idea about their size and the number of files you have ? what is the maximum size of a single line ? XML format for example may have just one line. But then I would expect OutOfMemory errors if this would be a problem. I hope the content is text. – Conffusion Sep 24 '20 at 07:37
  • Yes, content is in text format. And, now what I was trying to do, is done finally. I applied "Write" on those file data and it stored in a separate text file and it successfully wrote the content. But, still it is not displaying all the data in console but now there is no need to display data in console as I have a separate file now. So, thankyou for the help! – Sneh Raghav Sep 24 '20 at 11:16