将File对象添加到File对象的数组中

时间:2014-02-17 16:07:07

标签: java arrays file

首先,让我告诉你我到目前为止的代码:

public void populateArray(){

    //NOTE: THIS METHOD SHOULD BE RUN FIRST. Many of the methods in this class rely on a populated file array to function.

    // This method will populate our file array.
    // First, we note the directory we want to pull files from.
    File folder = new File("HR");
    File dir = new File("");
    File file = new File("");

    //Then we fill an array with the list of documents in that directory
    //This will also include folders.
    int count = 0;
    allDocs = folder.listFiles();
    int fileCount = 0;
    int dirCount = 0;
    System.out.println("Displaying contents of directory...");
    while (count < allDocs.length){
        System.out.println("Found: " + allDocs[count]);
        count++;
    }
    System.out.println("Sorting directories and files separately...");
    count = 0;
    while (count < allDocs.length){
        if (allDocs[count].isDirectory()){
            dir = new File(allDocs[count].getPath());
            dirs[dirCount] = dir;
            System.out.println("Document " + allDocs[count].getPath() + " sorted into -Directory- array at position " + dirCount);
            dirCount++;
        }
        else{
            file = new File(allDocs[count].getPath());
            files[fileCount] = file;
            System.out.println("Document " + allDocs[count].getPath() + " sorted into -File- array at position " + fileCount);
            fileCount++;
        }
        count++;
    }
    return;
}

files,dirs和allDocs是在任何方法之外的类中声明的数组。

allDocs是我用来获取感兴趣的目录HR中的每个目录和文件的数组。我使用allDocs = folder.listFiles();

执行此操作

我希望文件成为存储HR目录中所有文件的数组,我希望dirs是存储HR中所有目录的数组,但我不想存储任何内容人力资源部门的董事。

我尝试使用println“单独排序目录和文件”下的循环执行此操作。

问题在于:

else{
            file = new File(allDocs[count].getPath());
            files[fileCount] = file;
            System.out.println("Document " + allDocs[count].getPath() + " sorted into -File- array at position " + fileCount);
            fileCount++;
        }

这是因为allDocs中的第一个元素是一个文件。当第一个元素是目录时,也会出现此问题。问题是文件[fileCount] =文件上的空指针异常;线,我不明白。 “file”不为null,我只是给它赋了一个值。当然,在这种情况下,文件[fileCount]为null,但是如果我为它分配值,为什么会这样呢?

文件声明:

public class Methods
{

    private static File[] files;

2 个答案:

答案 0 :(得分:4)

您可能更愿意简化:

List<File> fileList = new ArrayList<>();
List<File> dirList = new ArrayList<>();
while (count < allDocs.length){
    if (allDocs[count].isDirectory()){
        dirList.add(allDocs[count]);
        System.out.println("Document " + allDocs[count].getPath() 
            + " sorted into -Directory- array at position " + dirCount);
        dirCount++;
    }
    else{
        fileList.add[allDocs[count]);
        System.out.println("Document " + allDocs[count].getPath()
            + " sorted into -File- array at position " + fileCount);
        fileCount++;
    }
    count++;
}
dirs = dirist.toArray(new File[dirList.size()]);
dirCount = dirs.length;
files = fileList.toArray(new File[fileList.size()]);
fileCount = files.length;

使用List<File>代替File[]似乎是合适的,因为您事先并不知道数组大小,并且您需要:

File[] files = new File[n];

如果打算填写文件:

files[fileCount] = ...

答案 1 :(得分:3)

您可能忘记分配数组 - 如下所示:

java.io.File [] files = new java.io.File[10];

如果您只是这样声明:

java.io.File [] files;

files将为nullfiles[fileCount]将导致NullPointerException

如评论中所述,您可能希望将其更改为:

java.util.List<java.io.File> files = new ArrayList<>();

并添加如下文件:

files.add(file);

这样您就不必提前知道参赛作品的数量。