41

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?

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
Martynogea
  • 559
  • 1
  • 6
  • 12
  • 1
    *"I'm quite sure I have other java files inside this folder"* Name them (so we can be more sure). – Andrew Thompson Mar 18 '13 at 16:57
  • 1
    I don't see anything wrong with your code, and your output. – Maroun Mar 18 '13 at 16:57
  • Please provide a listing of a comparable tool, like dir on windows or ls -l on unix, for comparision. – Ingo Mar 18 '13 at 16:59
  • 1
    It goes without saying, subdirectories are not listed and neither their contents. – Joop Eggen Mar 18 '13 at 16:59
  • 3
    *"Maybe the dot notation for current folder is incorrect?"* Print the result of [`File.getCanonicalFile()`](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getCanonicalFile%28%29) to check the path. – Andrew Thompson Mar 18 '13 at 16:59
  • Tell us what is it printing?? Is it printing any file name present in current directory ? – Vishal K Mar 18 '13 at 17:05
  • @AndrewThompson yes I do have files because that's where my java files are saved. In fact that's where the code I've posted here are saved. – Martynogea Mar 18 '13 at 17:05
  • It's printing .classpath then below is .project – Martynogea Mar 18 '13 at 17:06
  • Edit that information into the question. Have you checked the canonical path yet? Does it point to where you expect? Why am I having to even ask these questions? – Andrew Thompson Mar 18 '13 at 17:07
  • 1
    I think I might have found the problem. I assumed the current directory is at src folder (eclipse) but in reality the dot notation points to the folder outside of the src folder. However, src folder is the folder I save my files - therefore I assumed that'd be the current folder. Can anyone explain to me why src isn't the current folder? – Martynogea Mar 18 '13 at 17:09

7 Answers7

37

Try this,to retrieve all files inside folder and sub-folder

public static void main(String[]args)
    {
        File curDir = new File(".");
        getAllFiles(curDir);
    }
    private static void getAllFiles(File curDir) {

        File[] filesList = curDir.listFiles();
        for(File f : filesList){
            if(f.isDirectory())
                getAllFiles(f);
            if(f.isFile()){
                System.out.println(f.getName());
            }
        }

    }

To retrieve files/folder only

public static void main(String[]args)
    {
        File curDir = new File(".");
        getAllFiles(curDir);
    }
    private static void getAllFiles(File curDir) {

        File[] filesList = curDir.listFiles();
        for(File f : filesList){
            if(f.isDirectory())
                System.out.println(f.getName());
            if(f.isFile()){
                System.out.println(f.getName());
            }
        }

    }
Sachin
  • 3,424
  • 3
  • 21
  • 42
  • I guess here user is saying that he wants to print all the files present in `current Directory` .. Not in sub-directories too!! – Vishal K Mar 18 '13 at 17:02
12

Maybe the dot notation for current folder is incorrect?

Print the result of File.getCanonicalFile() to check the path.

Can anyone explain to me why src isn't the current folder?

Your IDE is setting the class-path when invoking the JVM.

E.G. (reaches for Netbeans) If you select menus File | Project Properties (all classes) you might see something similar to:

Netbeans project options

It is the Working Directory that is of interest here.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
3

You should verify that new File(".") is really pointing to where you think it is pointing - .classpath suggests the root of some Eclipse project....

Maroun
  • 94,125
  • 30
  • 188
  • 241
Gyro Gearless
  • 5,131
  • 3
  • 18
  • 14
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());
    }
Crispy91
  • 77
  • 1
  • 12
1

There is nothing wrong with your code. It should list all of the files and directories directly contained by the nominated directory.

The problem is most likely one of the following:

  • The "." directory is not what you expect it to be. The "." pathname actually means the "current directory" or "working directory" for the JVM. You can verify what directory "." actually is by printing out dir.getCanonicalPath().

  • You are misunderstanding what dir.listFiles() returns. It doesn't return all objects in the tree beneath dir. It only returns objects (files, directories, symlinks, etc) that are directly in dir.

The ".classpath" file suggests that you are looking at an Eclipse project directory, and Eclipse projects are normally configured with the Java files in a subdirectory such as "./src". I wouldn't expect to see any Java source code in the "." directory.


Can anyone explain to me why src isn't the current folder?"

Assuming that you are launching an application in Eclipse, then the current folder defaults to the project directory. You can change the default current directory via one of the panels in the Launcher configuration wizard.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • repost of my other comment, "I think I might have found the problem. I assumed the current directory is at src folder (eclipse) but in reality the dot notation points to the folder outside of the src folder. However, src folder is the folder I save my files - therefore I assumed that'd be the current folder. Can anyone explain to me why src isn't the current folder?" – Martynogea Mar 18 '13 at 17:17
0

Your code gives expected result,if you compile and run your code standalone(from commandline). As in eclipse for each project by default working directory is project directory that's why you are getting this result.

You can set user.dir property in java as:

   System.setProperty("user.dir", "absolute path of src folder");

then it will give expected result.

Avinesh
  • 535
  • 4
  • 10
0

I used this answer with my local directory ( for example E:// ) it is worked fine for the first directory and for the seconde directory the output made a java null pointer exception, after searching for the reason i discover that the problem was created by the hidden directory, and this directory was created by windows to avoid this problem just use this

public void recursiveSearch(File file ) {
 File[] filesList = file.listFiles();
    for (File f : filesList) {
        if (f.isDirectory() && !f.isHidden()) {
            System.out.println("Directoy name is  -------------->" + f.getName());
            recursiveSearch(f);
        }
        if( f.isFile() ){
            System.out.println("File name is  -------------->" + f.getName());
        }
    }
}
Yagami Light
  • 1,756
  • 4
  • 19
  • 39