2011-10-03 20 views
0

我有執行的Linux機器(ls - latr /home/ars | awk '{if(NR>1)print}')命令,給了我的目錄列表,用它的信息一起碼。我如何把它變成一些數組或列表,以便根據每個行,我可以得到的文件名,權限等從(列表或數組)!正確的方式把一個字符串到Java中的列表|常規

這裏是我的代碼,我的問題 這裏的底部打印輸出cmd=ls - latr /home/ars | awk '{if(NR>1)print}'

函數調用

HashMap<String,String> params = IntermediateResults.get("userparams") 
    Map env=AppContext.get(AppCtxProperties.environmentVariables) 
    def fClass = new GroovyClassLoader().parseClass(new File('plugins/infa9/Infa9CommandExecUtil.groovy')) 
    String cmd="ls -latr "+rrs.get("linkpath")+" | awk '{if(NR>1)print}'" 
    String res=fClass.newInstance().fetchInformation(params, env, cmd) 

我的函數調用

public String fetchInformation(Map<String,String> params, Map env, String cmd) 
{ 
try 
{ 
    Process proc = Runtime.getRuntime().exec(cmd); 
    InputStream stdin = proc.getInputStream(); 
    InputStreamReader isr = new InputStreamReader(stdin); 
    BufferedReader br = new BufferedReader(isr); 
    String line = null; 
    while ((line = br.readLine()) != null) 
    { 
     result.append(line); 
     println "$line" // This output is given at the end 
    } 
    int exitVal = proc.waitFor(); 

} 
catch (IOException io) 
{ 
    io.printStackTrace(); 
} 
catch (InterruptedException ie) 
{ 
    ie.printStackTrace(); 
} 
//println("\n\n"+result) 
return result 
} 

我的輸出

/data/u01/app/oracle/10.2.0/db_1: 
total 260 
-rwxr-xr-x 1 oracle dba  0 Jun 7 2005 root.sh.old 
drwxr-xr-x 4 oracle dba 4096 Jan 17 2007 xdk 
drwxr-xr-x 4 oracle dba 4096 Jan 17 2007 uix 
drwxr-xr-x 3 oracle dba 4096 Jan 17 2007 tg4tera 
drwxr-xr-x 3 oracle dba 4096 Jan 17 2007 tg4sybs 
drwxr-xr-x 3 oracle dba 4096 Jan 17 2007 tg4ingr 
drwxr-xr-x 3 oracle dba 4096 Jan 17 2007 tg4ifmx 

所以,我怎麼可以把上面的輸出在一些列表中,使每行,我可以得到權限,硬鏈接,所有者,組,文件大小,月份,日期,時間,一年中最後文件名

更新

這就是我要做的,是沒有更好的方法可以是通過使用地圖?

List<List<String>> frows = new ArrayList<List<String>>() 
    while ((line = br.readLine()) != null) 
    { 
     List<String> fileList= new ArrayList<String>() 
     result.append(line); 
     String[] strs = line.split(" ") 
     for(item in strs) 
     { 
      //print "$item " 
      fileList.add(item) 

     } 

     frows.add(fileList) 
    } 

    for (List<String> l : frows) 
    { 
     for (String s : l) { 
     print "$s" 
     } 
     println "" 
    } 
+0

這是一個固定寬度的輸出格式。因此,遍歷所有行並將字符串切成小塊(沿着列)。 – Howard

回答

2

創建一個類說FileDetail與性能權限,硬鏈接,所有者,組,文件大小,月份,日期,時間,一年和文件名。裏面的方法

List<FileDetail> fileDetails = new ArrayList<FileDetail>(); 
while ((line = br.readLine()) != null) 
    {    
     FileDetail file = new FileDetail(); 
     println "$line" // This output is given at the end 
     // parse - use space delimiter and String.split() API 
     String[] strs = line.split(" "); 
     // set values to "file" 
     // add to list 
     fileDetails .add(file); 
    } 
return fileDetails; 

使用地圖,

List<Map<String, String>> files = new ArrayList<Map<String, String>>() 
    while ((line = br.readLine()) != null) { 
      Map<String, String> file = new Map<String, String>()  
      String[] strs = line.split(" ")   
      // Find the order of fields, but it is system dependent, may not work in futrue   
      file.add("PERMISSIONS", strs[0]); // and so on for next and you may need to trim map value 
      files.add(file); 
      println "$line" 
    } 
return files; 
+0

凡'file'得到分裂字符串('「設定值到文件」')?也應該如何從列表中取回數據 abi1964

+0

我不能使用'列表 fileList = new ArrayList ;'而不是類? – abi1964

+0

同樣,您可能需要解析相同的'String'來獲取詳細信息,如權限,所有者等。相反,您可以解析並將其存儲在Object中,並隨時隨地使用。 – Vaandu

4

您不應該使用系統相關的ls命令,並解析它的容易出錯的輸出。例如,考慮一個文件名 「富\ n drwxr-XR-X 4的Oracle DBA 4096 2007年1月17日UIX」。

使用的java.io.File,請參閱相關的目錄,用名稱來獲取文件的對象,時間屬性。

您使用列表如下:

List <File> result = new List<File>(); 

    // and in the loop: 
    result.add (file); 
1

另一種方法是使用JNA訪問本地stat呼籲平臺...

這對我的作品在OS X 。我將它保存爲stattest.groovy,當執行groovy stattest.groovy時,它將打印出有關其本身的詳細信息:

@Grab('net.java.dev.jna:jna:3.3.0') 
@Grab('org.jruby.ext.posix:jna-posix:1.0.3') 
import org.jruby.ext.posix.* 

File f = new File('stattest.groovy') 
POSIX posix = POSIXFactory.getPOSIX([ isVerbose:{ false } ] as POSIXHandler , true) 
FileStat s = posix.stat(f.absolutePath) 

s.properties.each { name, val -> 
    println "$name: $val" 
} 

[ 'atime', 'blocks', 'blockSize', 'ctime', 'dev', 'gid', 'ino', 'mode', 'mtime', 'nlink', 'rdev', 'st_size', 'uid' ].each { 
    println "$it() -> ${s."$it"()}" 
} 

打印輸出:

13:23:46 [ty[email protected]] JNA $ groovy stattest.groovy 
symlink: false 
file: true 
setuid: false 
byteBuffer: java.nio.HeapByteBuffer[pos=0 lim=120 cap=120] 
executableReal: false 
structSize: 120 
namedPipe: false 
empty: false 
blockDev: false 
executable: false 
fifo: false 
class: class org.jruby.ext.posix.MacOSHeapFileStat 
setgid: false 
sticky: false 
charDev: false 
owned: true 
directory: false 
ROwned: true 
readableReal: true 
writableReal: true 
readable: true 
writable: true 
groupOwned: false 
socket: false 
atime() -> 1317644640 
blocks() -> 8 
blockSize() -> 4096 
ctime() -> 1317644636 
dev() -> 234881026 
gid() -> 1255 
ino() -> 62213399 
mode() -> 33204 
mtime() -> 1317644636 
nlink() -> 1 
rdev() -> 0 
st_size() -> 527 
uid() -> 1114 
相關問題