File.list() vs File.listFiles()
Asked Answered
L

2

14

My question is: if this two functions have something different? I mean I know that they return something different, but is it possible that number of elements in one would be different then in the second one. I will try to explain. I implemented TreeModel for one of my class trying to make nice view on files on the PC basing on JTree. So here is the part of it:

public Object getChild(Object parent, int index) {
        File[] children = ((File) parent).listFiles();
        if(children == null || index < 0 || index >= children.length) {
            return null;
        }

        File result = new MyFile(children[index]);
        return result;
}

public int getChildCount(Object parent) {
        //---
        //String[] children = ((File)parent).list();
        File[] children = ((File)parent).listFiles();
        //---

        if(children == null) {
            return 0;
        }
        return children.length;
}

I marked interesting code. If I changed this two lines for this commented one, sometimes I get NullPointerException after loading TreeModel: jtree.setModel(treeModel);. This uncommented does not cause any trouble. I checked the docs and it says nothing unusual including returning null by both methods. What is going on here?

Lanark answered 12/3, 2013 at 18:55 Comment(1)
The only reason they might return different results would because the state of the file system changed between the calls (a file was added/removed). The only reason you old get a NPE from File#listFiles is because the either the path that the file object represents does not exist or is not a directoryRorke
T
9

Both methods do essentially the same, look at http://www.docjar.com/html/api/java/io/File.java.html for details.

Till answered 12/3, 2013 at 18:58 Comment(3)
So the question is: what is going on with this model?Lanark
maybe you just have been lucky with using listFiles()? One method is based upon the other, so they should return consistent data. where exactly do you get the NPE anyways if you have the children==null-if? Maybe parent is null? Whcih variable is null?Till
the same ... except that the listFiles() method returns a File[], whereas the list() method just returns the file names (i.e. a String[]).Boater
E
2

As already pointed, but clarified only within the comments in post from D.R

  • list method returns String array with filenames (files and directories)

  • listFiles return array of class File of the same

See doc pages, eg. https://docs.oracle.com/javase/7/docs/api/java/io/File.html

String[] list() Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.

File[] listFiles() Returns an array of abstract pathnames denoting the files in the directory denoted by this abstract pathname.

I am not sure why both methods exist, probably String array will be faster and less memory consuming than File array one

Exorcist answered 27/2, 2021 at 18:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.