Daniel Lee
Daniel Lee

Reputation: 25

Reading multiple txt files from multiple folders in Java

This is the data structure in my computer.

amdar(folder) / 20141125(folder) / 300++ txt files
              / 20141126(folder) / 300++ txt files
              / 20141127(folder) / 300++ txt files ...
              / 20141128(folder) / 300++ txt files ...
              / 20141129(folder) / 300++ txt files ...
             ... daily folders are added on and on ...   `

I want to read each folder along with 300++ txt files with Java. For doing so, I used two loops: one for folders and another for txt files of the folders. First for loop for the folders worked well. Second nested for loop for the txt files went awry. While the folders begin from 20141125, the read txt files started from somewhere in the 20141128 folder; so many txt files in the folders: 20141125 ~ 20141128 were not read. Please see the screenshot below.

[Screenshot: Why are the txt files from the 20141125 folder not shown first and even not shown at all?]

To solve this issue, instead of nested for loop, I tried to use while and separate method, but the result(error), not starting to read from the beginning folder, was the same. Based on the code below, I don't find any reason making this issue happen. Can you explain why? Thank you.

    public static void main(String[] args) {
        File dir = new File("c:\\html_test\\amdar");
        File[] folder = dir.listFiles();
            for(File table : folder) {
                System.out.println(table);
                File[] filenames = table.listFiles();
                    for (File file : filenames) {
                        System.out.println(file);
                    }
            }
    }

Upvotes: 1

Views: 1911

Answers (2)

Daniel Lee
Daniel Lee

Reputation: 25

Right click on the console > Preferences > Console buffer size.
> Uncheck the "Limit console output" checkbox.

There was no error in my code.

It was just that the result had been truncated by the 80000 character limit.

I released the limit and got the result that I expected.

Upvotes: 1

Varsha Nadar
Varsha Nadar

Reputation: 41

Code written for to read multiple test files from multiple folder: 
In the try block the FileReader reads a file and the second works as a constructor parameter and has a readline() method.


    public static void main(String[] args) {
            String dir = "c:\\html_test\\amdar";
        File dir = new File(dir);
            File[] folder = dir.listFiles();

        for(File table : folder) {
        if(table.isFile())
        {  
            BufferedReader inputStream = null;      
        try{
            inputStream = new BufferedReader(new FileReader(f));
            String line;
            while ((line = inputStream.readLine()) != null) 
             {
               System.out.println(line);
              }

            }  
             finally {
                        if (inputStream != null) {
                        inputStream.close();
                        }           

                     }
        }
              }
    }

Upvotes: 0

Related Questions