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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21833854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 10:58:38  来源:igfitidea点击:

Add a File object to an array of File objects

javaarraysfile

提问by Lighthat

First, let me show you the code I have so far:

首先,让我向您展示我目前拥有的代码:

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, and allDocs are arrays declared within the class outside of any methods.

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

allDocs is the array I use to get every directory and file in the directory of interest, HR. I do this with allDocs = folder.listFiles();

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

I want files to be the array to store all of the files in the directory HR, and I want dirs to be the array to store all of the directories in HR, but I don't want to store any of the contents of the directors in HR.

我希望 files 是存储目录 HR 中所有文件的数组,我希望 dirs 是存储 HR 中所有目录的数组,但我不想存储导演的任何内容在人力资源部。

I try to do this with the loop under the println "Sorting directories and files separately."

我尝试使用 println“分别对目录和文件进行排序”下的循环来执行此操作。

The problem is here:

问题在这里:

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++;
        }

It's here because the first element in allDocs is a file. The problem also occurs when the first element is a directory. The problem is a null pointer exception on the files[fileCount] = file; line, which I don't understand. "file" is not null, I just assigned it a value. Of course files[fileCount] is null in this case, but why does that matter if I'm assigning it a value?

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

Files declaration:

文件声明:

public class Methods
{

    private static File[] files;

采纳答案by Joop Eggen

You might prefer to simplify:

您可能更喜欢简化:

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;

Using a List<File>instead of File[]seems appropiate, as you do not know the array sizes in advance, and you need:

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

File[] files = new File[n];

if intending to fill in a File:

如果打算填写文件:

files[fileCount] = ...

回答by Rainer Schwarze

You might have forgotten to allocate the array - like this:

您可能忘记分配数组 - 像这样:

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

If you only declared it like this:

如果你只是这样声明:

java.io.File [] files;

fileswill be nulland files[fileCount]will cause a NullPointerException.

files将会null并且files[fileCount]将会导致NullPointerException.

As mentioned in the comments, you might like to change it to:

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

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

and add the files like:

并添加如下文件:

files.add(file);

This way you won't have to know the number of entries in advance.

这样您就不必提前知道条目的数量。