*Nix ls Command in Java

前端 未结 5 873
野的像风
野的像风 2021-01-24 00:54

Anyone aware of a method/class/library that will allow me to easily reproduce the results of the *nix ls -l command in Java? Calling ls directly is not an option due to platform

相关标签:
5条回答
  • 2021-01-24 01:18

    Here is a tutorial on getting a directory listing in java with sample source code.

    0 讨论(0)
  • 2021-01-24 01:25

    This provides what you are looking for:

    0 讨论(0)
  • 2021-01-24 01:26

    You can use the java.nio.file package.

    0 讨论(0)
  • 2021-01-24 01:34

    Here's an implementation of ls -R ~, which lists files recursively starting in the home directory:

    import java.io.*;
    
    public class ListDir {
    
        public static void main(String args[]) {
            File root;
            if (args.length > 0) root = new File(args[0]);
            else root = new File(System.getProperty("user.dir"));
            ls(root); 
        }
    
        /** iterate recursively */
        private static void ls(File f) { 
            File[] list = f.listFiles();
            for (File file : list) {
                if (file.isDirectory()) ls(file);
                else System.out.println(file);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-24 01:40

    I think we don't have ready to use Java class in java stadard library. But you can develop a tool like *nix ls -l tool by using classes in java.io package.

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