Que*_*ier 8 java linux command-line ls
我想在java中打印"*.C"文件
我使用下面的代码
public static void getFileList(){
try
{
String lscmd = "ls *.c";
Process p=Runtime.getRuntime().exec(lscmd);
p.waitFor();
BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream()));
String line=reader.readLine();
while(line!=null)
{
System.out.println(line);
line=reader.readLine();
}
}
catch(IOException e1) {
System.out.println("Pblm found1.");
}
catch(InterruptedException e2) {
System.out.println("Pblm found2.");
}
System.out.println("finished.");
}
Run Code Online (Sandbox Code Playgroud)
PS: - 它对"ls"命令工作正常.当我在任何命令中使用"*"时,一切都不起作用.需要在java中的命令中替换"*".
UPDATE
谢谢你的帮助.
现在我需要在java中注释"ls -d1 $ PWD/**"的相同结果.它将列出具有完整路径的所有目录名称.
谢谢你的时间.
VGR*_*VGR 13
您可能会发现这更可靠:
Path dir = Paths.get("/path/to/directory");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.c")) {
for (Path file : stream) {
// Do stuff with file
}
}
Run Code Online (Sandbox Code Playgroud)