java | groovy中将字符串放入列表的正确方法
我有一个代码,它在Linux框中执行一个命令(java | groovy中将字符串放入列表的正确方法,java,regex,linux,groovy,Java,Regex,Linux,Groovy,我有一个代码,它在Linux框中执行一个命令(ls-latr/home/ars | awk'{if(NR>1)print}),它给了我目录列表及其信息。如何将其放入某个数组或列表中,以便根据每一行从(列表或数组)中获取文件名、权限等 这是我的代码,它在问题的底部打印输出 这里cmd=ls-latr/home/ars | awk'{if(NR>1)print}' 函数调用 HashMap<String,String> params = IntermediateResults.get("
ls-latr/home/ars | awk'{if(NR>1)print}
),它给了我目录列表及其信息。如何将其放入某个数组或列表中,以便根据每一行从(列表或数组)中获取文件名、权限等
这是我的代码,它在问题的底部打印输出
这里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 )
那么,我如何将上述输出放在一些列表中,以便在每一行上都可以获得权限、硬链接、所有者、组、文件大小、月份、日期、时间、年份以及最终的文件名
更新
这就是我想做的,有没有更好的方法可以使用地图
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 ""
}
List frows=new ArrayList()
而((line=br.readLine())!=null)
{
List fileList=new ArrayList()
结果。追加(行);
字符串[]strs=line.split(“”)
用于(strs中的项目)
{
//打印“$item”
文件列表。添加(项)
}
添加(文件列表)
}
对于(列表l:frows)
{
for(字符串s:l){
打印“$s”
}
println“”
}
您不应该使用依赖于系统的ls命令,并解析其容易出错的输出。考虑一个文件名“Foo\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);
列表结果=新列表();
//在循环中:
result.add(文件);
创建一个类,比如说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 fileDetails=new ArrayList();
而((line=br.readLine())!=null)
{
FileDetail file=newfiledetail();
println“$line”//此输出在末尾给出
//parse-使用空格分隔符和String.split()API
字符串[]strs=line.split(“”);
//将值设置为“文件”
//添加到列表中
fileDetails.add(文件);
}
返回文件详细信息;
使用地图
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;
List files=new ArrayList()
而((line=br.readLine())!=null){
映射文件=新映射()
字符串[]strs=line.split(“”)
//查找字段的顺序,但它依赖于系统,可能在将来不起作用
file.add(“PERMISSIONS”,strs[0]);//等等,接下来可能需要修剪映射值
文件。添加(文件);
println“$line”
}
归还文件;
另一种选择是使用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 [tyates@mac] 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
这是一种固定宽度的输出格式。因此,迭代所有行并将字符串切成碎片(沿列)。哪里是
file
获取拆分的字符串(“将值设置为file”
)?还有,我应该如何从列表中获取数据?我不能使用List fileList=new ArrayList代码>而不是类?同样,您可能需要解析相同的字符串
以获取权限、所有者等详细信息。相反,您可以解析并将其存储在对象
中,并在需要的任何位置使用。嘿,感谢您的评论,您也可以查看我的更新问题,我将代码放在其中,但使用列表,在地图的帮助下有更好的方法吗?
@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 [tyates@mac] 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