3

使用 ls 我得到以下信息。如何从 android 中的文件对象中获取相同的内容。就像如何使用文件对象获取 u0_a23 名称字符串一样。

root@android:/ # ls /proc/ -l                                                  
dr-xr-xr-x root     root              2013-07-01 12:19 1
dr-xr-xr-x root     root              2013-07-01 12:19 10
dr-xr-xr-x root     root              2013-07-01 12:31 1069
dr-xr-xr-x root     root              2013-07-01 12:19 11
dr-xr-xr-x root     root              2013-07-01 12:19 12
dr-xr-xr-x root     root              2013-07-01 12:19 13
dr-xr-xr-x root     root              2013-07-01 12:19 14
dr-xr-xr-x root     root              2013-07-01 12:19 2
dr-xr-xr-x root     root              2013-07-01 12:19 25
dr-xr-xr-x root     root              2013-07-01 12:19 26
dr-xr-xr-x root     root              2013-07-01 12:19 27
dr-xr-xr-x root     root              2013-07-01 12:19 28
dr-xr-xr-x root     root              2013-07-01 12:19 29
dr-xr-xr-x root     root              2013-07-01 12:19 290
dr-xr-xr-x root     root              2013-07-01 12:19 292
dr-xr-xr-x system   system            2013-07-01 12:19 294
dr-xr-xr-x root     root              2013-07-01 12:19 3
dr-xr-xr-x system   system            2013-07-01 12:19 30
dr-xr-xr-x root     root              2013-07-01 12:19 31
dr-xr-xr-x root     root              2013-07-01 12:19 33
dr-xr-xr-x root     root              2013-07-01 12:19 34
dr-xr-xr-x radio    radio             2013-07-01 12:19 35
dr-xr-xr-x u0_a23   u0_a23            2013-07-01 12:20 357
4

2 回答 2

0

在 Java 7 中有Files#getOwner,否则,正如 vRallev 所说,唯一的方法是使用 Process。

尝试一个进程,看看有多快(在我的设备上,处于调试模式的 Galaxy S2 只需要 40 毫秒。我猜没那么多)。

我试图用文件信息实现一个小对象 FileInfo。也许你会发现它很有用。我不确定它是否每次都能正常工作(我假设唯一缺少的参数可能是大小和类似的东西),但这可能是一个开始。

public static void test() {
    long start = System.currentTimeMillis();
    BufferedWriter out;
    BufferedReader in;
    String test = "";
    List<FileInfo> fileInfoList = new ArrayList<FileInfo>();
    try {
        Process proc = Runtime.getRuntime().exec("ls -l");
        out = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));
        in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
        String line = "";
        while((line = in.readLine()) != null) {
            fileInfoList.add(createFileInfo(line.split("\\s+")));
        }
        proc.waitFor();
        in.close();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    long execTime = System.currentTimeMillis() - start;
    System.out.println(execTime);
}

private static FileInfo createFileInfo(String... args) {
    FileInfo fi = null;
    if(args.length == 6) {
        fi = new FileInfo(args[0],args[1],args[2],args[3] +" "+ args[4],args[5]);
    } else if(args.length == 7) {
        fi = new FileInfo(args[0],args[1],args[2],args[3],args[4] +" "+ args[5],args[6]);
    }
    return fi;
}

private static class FileInfo {
    private String permissions;
    private String owner;
    private String group;
    private String size;
    private String date;
    private String fileName;

    private FileInfo(String permissions, String owner, String group, String size, String date, String fileName) {
        this.permissions = permissions;
        this.owner = owner;
        this.group = group;
        this.size = size;
        this.date = date;
        this.fileName = fileName;
    }

    private FileInfo(String permissions, String owner, String group, String date, String fileName) {
        this.permissions = permissions;
        this.owner = owner;
        this.group = group;
        this.date = date;
        this.fileName = fileName;
    }
}
于 2013-07-01T16:08:27.313 回答
0

您可以创建一个 Process 对象并执行该命令。您可以附加 File 对象的路径作为参数。之后,您可以从 InputStream 中读取结果。

如果您需要示例,请发表评论。

于 2013-07-01T13:04:46.273 回答