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

Lig*_*hat 6 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;
}
Run Code Online (Sandbox Code Playgroud)

文件,目录和allDocs是在任何方法之外的类中声明的数组。

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

我希望文件成为将所有文件存储在HR目录中的数组,而我希望Dirs成为将所有目录存储在HR中的数组,但是我不想存储任何Director的内容在人力资源中。

我尝试使用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++;
        }
Run Code Online (Sandbox Code Playgroud)

之所以在这里,是因为allDocs中的第一个元素是一个文件。当第一个元素是目录时,也会发生此问题。问题是files [fileCount] = file上的空指针异常;行,我不明白。“文件”不为空,我只是为其分配了一个值。当然,在这种情况下,files [fileCount]为null,但是如果我为其分配值,那又为什么呢?

文件声明:

public class Methods
{

    private static File[] files;
Run Code Online (Sandbox Code Playgroud)

Joo*_*gen 5

您可能希望简化:

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;
Run Code Online (Sandbox Code Playgroud)

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

File[] files = new File[n];
Run Code Online (Sandbox Code Playgroud)

如果打算填写文件:

files[fileCount] = ...
Run Code Online (Sandbox Code Playgroud)