*Nix ls Command in Java

2019-02-28 17:43发布

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 independence.

eg. $ls -l myprogram.exe

-rwxrwxrwx 1 auser None 1261568 Nov 15 17:41 C:\myprogram.exe

标签: java shell
5条回答
淡お忘
3楼-- · 2019-02-28 18:17

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.

查看更多
做自己的国王
4楼-- · 2019-02-28 18:17

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);
        }
    }
}
查看更多
疯言疯语
6楼-- · 2019-02-28 18:22

You can use the java.nio.file package.

查看更多
登录 后发表回答