我有執行的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 ""
}
這是一個固定寬度的輸出格式。因此,遍歷所有行並將字符串切成小塊(沿着列)。 – Howard