Alex Rivera | Logout

How to list the files in current directory?

Asked 2013-03-18T16:54:47.893
42

I want to be able to list the files in the current directory. I've made something that should work but doesn't return all the file names.

File dir = new File(".");
File[] filesList = dir.listFiles();
for (File file : filesList) {
    if (file.isFile()) {
        System.out.println(file.getName());
    }
}

It returns .classpath, but I'm quite sure I have other java files inside this folder. Maybe the dot notation for current folder is incorrect?

Edit
Report

1 Answer

3

Had a quick snoop around for this one, but this looks like it should work. I haven't tested it yet though.

    File f = new File("."); // current directory

    File[] files = f.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            System.out.print("directory:");
        } else {
            System.out.print("     file:");
        }
        System.out.println(file.getCanonicalPath());
    }
answered 2013-03-18T17:00:46.347

Your Answer