如何遍历目录,获取所有文件名,然后以Java

时间:2015-09-29 13:27:19

标签: java file exception

我是新来的。如果格式不正确,请原谅我。

所以在C盘中,我有一个名为Search Files的文件夹,在Search Files内,我有四个名为Folder 1Folder 2Folder 3的子目录,以及Folder 4

Folder 1内,我有一个名为hello.txt的文本文件,该文件的内容为hello

我的预期输出为"Directory of the file" + "file name" + "file body"

以下是我现在的代码。

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());
        }*/
    }
}

当我执行上面的代码时,我收到以下错误:

 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)

我需要再次获取文件目录,文件名和文件内容。

如果hello.txt,预期输出应为: C:\ Search Files \ Folder1 \ hello.txt hello

你们能指出我正确的方向吗?如何修复上述异常以及如何获取显示目录,文件名和文件内容的String?

先谢谢。

1 个答案:

答案 0 :(得分:1)

您的代码看起来没问题,除非您必须按如下方式组织它,尤其是showFiles方法。

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();
    }

输出结果如下:

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