How to list the files in current directory?

前端 未结 7 2114
傲寒
傲寒 2021-02-05 01:05

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(\         


        
相关标签:
7条回答
  • 2021-02-05 01:25

    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.

    0 讨论(0)
  • 2021-02-05 01:25

    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.

    0 讨论(0)
  • 2021-02-05 01:36

    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());
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-05 01:47

    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.

    0 讨论(0)
  • 2021-02-05 01:49

    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());
        }
    
    0 讨论(0)
  • 2021-02-05 01:50

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

    0 讨论(0)
提交回复
热议问题