如何获得Android的文件所有权和权限 [英] How to get file ownership and permissions in android

查看:141
本文介绍了如何获得Android的文件所有权和权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ls命令我得到以下信息。如何从一个文件对象相同的机器人。
怎么样使用File对象获取u0_a23名字符串。

 根@安卓/#LS的/ proc / -l
DR-XR-XR-X根根2013-07-01 12:19 1
DR-XR-XR-X根根2013-07-01 12点19 10
DR-XR-XR-X根根2013-07-01 12:31 1069
DR-XR-XR-X根根2013-07-01 12:19 11
DR-XR-XR-X根根2013-07-01 12:19 12
DR-XR-XR-X根根2013-07-01 12:19 13
DR-XR-XR-X根根2013-07-01 12:19 14
DR-XR-XR-X根根2013-07-01 12:19 2
DR-XR-XR-X根根2013-07-01 12:19 25
DR-XR-XR-X根根2013-07-01 12:19 26
DR-XR-XR-X根根2013-07-01 12点19 27
DR-XR-XR-X根根2013-07-01 12:19 28
DR-XR-XR-X根根2013-07-01 12:19 29
DR-XR-XR-X根根2013-07-01 12时19 290
DR-XR-XR-X根根2013-07-01 12:19 292
DR-XR-XR-X系统的系统2013-07-01 12点19分294
DR-XR-XR-X根根2013-07-01 12:19 3
DR-XR-XR-X系统的系统2013-07-01 12:19 30
DR-XR-XR-X根根2013-07-01 12:19 31
DR-XR-XR-X根根2013-07-01 12点19 33
DR-XR-XR-X根根2013-07-01 12:19 34
DR-XR-XR-X无线电台2013-07-01 12:19 35
DR-XR-XR-X u0_a23 u0_a23 2013-07-01 12:20 357


解决方案

在Java 7中有<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#getOwner%28java.nio.file.Path,%20java.nio.file.LinkOption...%29\"相对=nofollow>文件#getOwner ,否则唯一的办法就是,作为vRallev说,一个过程。

尝试过程看到的是如何快速(我的设备上,银河S2在调试模式下,只需40毫秒。与其说我猜的)。

我试图实现一个小的FileInfo对象的文件信息。也许你能发现它有用。我不知道它会工作每次(我假设的唯一参数丢失可能是这样的大小和东西),但它可能是一个开始。

 公共静态无效测试(){
    长启动= System.currentTimeMillis的();
    BufferedWriter将出;
    在的BufferedReader;
    字符串测试=;
    清单&LT;&的FileInfo GT; fileInfoList =新的ArrayList&LT;&的FileInfo GT;();
    尝试{
        流程PROC =调用Runtime.getRuntime()EXEC(ls -l命令);
        OUT =新的BufferedWriter(新OutputStreamWriter(proc.getOutputStream()));
        在=新的BufferedReader(新的InputStreamReader(proc.getInputStream()));
        串线=;
        而((行= in.readLine())!= NULL){
            fileInfoList.add(createFileInfo(line.split(\\\\ S +)));
        }
        proc.waitFor();
        附寄();
        out.close();
    }赶上(例外五){
        e.printStackTrace();
    }
    长execTime = System.currentTimeMillis的() - 启动;
    的System.out.println(execTime);
}私有静态的FileInfo createFileInfo(字符串参数... args){
    FileInfo的网络连接= NULL;
    如果(args.length == 6){
        音响=新的FileInfo(参数[0],ARGS [1],ARGS [2],ARGS [3] ++ ARGS [4],ARGS [5]);
    }否则如果(args.length == 7){
        音响=新的FileInfo(参数[0],ARGS [1],ARGS [2],ARGS [3],ARGS [4] ++ ARGS [5],ARGS [6]);
    }
    返回网络;
}私有静态类的FileInfo {
    私人字符串的权限;
    私人字符串业主;
    私人字符串组;
    私人字符串大小;
    私人字符串日期;
    私人字符串文件名;    私人的FileInfo(权限字符串,字符串所有者,弦乐组,字符串大小,字符串日期,字符串文件名){
        this.permissions =权限;
        this.owner =所有者;
        this.group =组;
        this.size =大小;
        this.date =日期;
        this.fileName =文件名;
    }    私人的FileInfo(权限字符串,字符串所有者,弦乐组,字符串日期,字符串文件名){
        this.permissions =权限;
        this.owner =所有者;
        this.group =组;
        this.date =日期;
        this.fileName =文件名;
    }
}

Using ls I get the following information. How to get the same from a File Object in android. Like How to get u0_a23 name string using File Object.

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

解决方案

In Java 7 there's Files#getOwner, otherwise the only way is, as vRallev said, with a Process.

Try a Process to see how fast is (on my device, a Galaxy S2 in DEBUG mode, takes just 40ms. Not so much I guess).

I've tried to implement a little Object FileInfo with the file information. Maybe you can find it useful. I'm not sure it will work everytime (I'm assuming that the only parameter missing could be the size and stuff like that) but it could be a start.

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;
    }
}

这篇关于如何获得Android的文件所有权和权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆