简体   繁体   中英

How to loop through a directory, get all file names, then get the contents of those files in Java

I am new here. Forgive me if I have any incorrect formatting.

So in C drive, I have a folder called Search Files and inside Search Files , I have four subdirectories called Folder 1 , Folder 2 , Folder 3 , and Folder 4 .

Inside of Folder 1 , I have a text file called hello.txt and the contents of that file is hello .

My expected output is "Directory of the file" + "file name" + "file body" .

Below is the code that I have right now.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;

import org.apache.commons.io.FileUtils;

public class FileDirectories {

    public static void main(String[] args) throws IOException {
        File[] files = new File("C:\\Search Files").listFiles();
        showFiles(files);
    }

    public static void showFiles(File[] files) throws IOException {
        String line = null;

            try{
                for (File file : files) {
                    if (file.isDirectory()) {
                        String fileName = "Directory" + file.getName();
                        //System.out.println("Directory: " + file.getName());
                        BufferedReader in = new BufferedReader(new FileReader(file));
                        while((line = in.readLine()) != null)
                        {
                            System.out.println(line);
                        }
                        in.close();
                        showFiles(file.listFiles()); // Calls same method again.
                    } else {
                        System.out.println("File: " + file.getName() + file.toString());
                    }
                }
            }catch(NullPointerException e){
                e.printStackTrace();
            }




        /*Iterator<File> it = FileUtils.iterateFiles(new File("C://Search Files//"), null, false);
        while(it.hasNext()) {
            System.out.println(((File) it.next()).getName());
        }*/
    }
}

When I execute the above code, I get the following error:

 Exception in thread "main" java.io.FileNotFoundException: C:\Search   Files\Folder 1 (Access is denied)
        at java.io.FileInputStream.open(Native Method)
        at java.io.FileInputStream.<init>(FileInputStream.java:146)
        at java.io.FileReader.<init>(FileReader.java:72)
        at org.raghav.stuff.FileDirectories.showFiles(FileDirectories.java:27)
        at org.raghav.stuff.FileDirectories.main(FileDirectories.java:16)

Once again, I need to get the file directory, the file name, and the contents of the file.

In case of the hello.txt , the expected output should be: C:\\Search Files\\Folder1\\ hello.txt hello

Can you guys point me in the right directions? How to fix the above exception and how do I get a String that displays the directory, file name, and the contents of the file?

Thanks in advanced.

Your code looks okay except you have to organized it as follows especially showFiles method.

public static void showFiles(File[] files) throws IOException {
    String line = null;

    try{
        for (File file : files) {
            if (file.isDirectory()) {
                String fileName = "Directory: " + file.getName();
                System.out.print(fileName);
                showFiles(file.listFiles()); // Calls same method again.
            } else {
                System.out.print("\tFile: " + file.getName() + file.toString());
                //System.out.println("Directory: " + file.getName());
                BufferedReader in = new BufferedReader(new FileReader(file));
                while((line = in.readLine()) != null)
                {
                    System.out.print("\t Content:" + line);
                }
                in.close();
                System.out.println();
            }
        }
    }catch(NullPointerException e){
        e.printStackTrace();
    }

And the output will look like:

Directory: Folder 1 File: C:\Search Files\Folder 1\test.txt  Content:this is a test
Directory: Folder 2 File: C:\Search Files\Folder 2\test.txt  Content:this is a test
Directory: Folder 3 File: C:\Search Files\Folder 3\test.txt  Content:this is a test
Directory: Folder 4 File: C:\Search Files\Folder 4\test.txt  Content:this is a test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM