将一个文件对象添加到文件对象数组中。

7

首先,让我展示一下我目前拥有的代码:

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(); 来实现这一点。

我想让files成为存储目录HR中所有文件的数组,而将dirs作为存储HR中所有目录的数组,但我不想存储HR目录中任何内容的目录。

我尝试通过println "Sorting directories and files separately." 下的循环来实现此目标。

问题出在这里:

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中的第一个元素是文件。当第一个元素是目录时,问题也会出现。问题在于文件files[fileCount] = file;行产生了空指针异常,我不明白原因。虽然“file”不为空,但我仍然赋予它一个值。当然,在这种情况下,files[fileCount]为空,但如果我给它赋值,那么这有什么关系呢?

文件声明:

public class Methods
{

    private static File[] files;

告诉你我想做什么以及为什么不能做,这不够具体吗?我没有更多可以告诉你的了。 - ozzymado
1
有什么原因你不能使用 List<File> 而不是数组?这会使所有的事情变得更简单...但听起来是 files 为空,而不是 file - Jon Skeet
1
从上面的代码看来,files变量似乎没有被初始化,因此导致了NPE。 - wypieprz
1
由于您的问题直接涉及到“files”变量,因此看一下它的声明会非常有帮助。 - Alex Wittig
1
如果它是空的,那么你没有给它赋值。你正在给一个不存在的数组赋值。在你能够给它的索引分配值之前,你必须先创建这个数组。 - Rudi Kershaw
显示剩余3条评论
2个回答

7
您可能更喜欢简化:

您可能更喜欢简化:

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] = ...

4
您可能忘记分配数组 - 就像这样:
java.io.File [] files = new java.io.File[10];

如果你只是像这样声明它:
java.io.File [] files;

files将会是null,而files[fileCount]会导致NullPointerException

如在评论中所提到的,你可能想要更改为:

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

然后添加文件,如下:

files.add(file);

这样你就不必提前知道条目数量了。


你比我早几秒钟回答了。+1 - Rudi Kershaw
1
那就是问题所在了...我没有给它一个大小,因为我成功地使用allDocs.listFiles()填充了allDocs,但我猜这是因为listFiles()返回一个数组,而我正在尝试将单个对象包含到一个数组中。谢谢。 - ozzymado

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接