2013-07-01 5 views
2

ls를 사용하여 다음 정보를 얻습니다. 안드로이드에있는 File Object에서 같은 것을 얻는 방법. Like File 객체를 사용하여 u0_a23 이름 문자열을 얻는 방법.android에서 파일 소유권 및 사용 권한을 얻는 방법

[email protected]:/ # 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 
+0

몇 가지 정보로 답변을 업데이트했습니다. :) – Enrichman

답변

0

Process 개체를 만들고 명령을 실행할 수 있습니다. File 객체의 경로를 매개 변수로 추가 할 수 있습니다. 그런 다음 InputStream에서 결과를 읽을 수 있습니다.

예가 필요한 경우 의견을 보내주십시오.

+0

좋은 해킹,하지만 10Ms 미만의 정보를 얻으려면 내 경우에는 적합하지 않습니다. – mSO

+0

아직 시간을 측정하지 않았습니다. 정말 오래 걸릴까요? – vRallev

+0

/proc은 현재 실행중인 pid의 목록을 표시합니다. 그래서 그들은 이미 달리고 있습니다. 또한 내가 다시 실행할 수 있는지 확실하지 않다. a) 내가 할 수있다. b) 이제는 앱 소유자가 새로 생성 된 프로세스의 소유자가 될 것이므로, 내 목적을 해결할 수있을 것이다. c) 얼마나 오래 걸릴지 모른다. – mSO

0

Java 7에는 Files#getOwner이 있습니다. 그렇지 않으면 vRallev가 말한 것처럼 프로세스가있는 유일한 방법입니다.

얼마나 빨리 볼 수 있습니까? (내 장치에서 DEBUG 모드의 Galaxy S2는 40ms 밖에 걸리지 않습니다.)

파일 정보로 작은 Object 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; 
    } 
} 
+0

나는 그것을 할 수있다. 그것은 내가 아무런 효과가 없다면 내가 생각한 마지막 척도이다. :) 나는 NDK를 통해 한 가지 더 길을 가지지 만 그 여분의 마일을가는 것이 의미가 있을지 확신하지 못한다. – mSO

+0

그건 나에게 너무 복잡해 보인다. 프로세스에서 성능이 좋지 않은 것 같습니다. RootTools 라이브러리에서 루트 권한이 필요한 응용 프로그램을 실제로 개발하고 있는데 성능이 꽤 좋습니다. 불행히도 그것은 당신이 (그리고 나) 무엇을하려고하는지 확실히 "유닉스 (unix)"스 프시픽 (spcific) 한 것입니다. 그래서 이것이 이것보다 나은 것은 없습니다. :) – Enrichman

관련 문제