2013-03-24 27 views
8

我想打印 “* .C” 在java文件如何在java中使用“ls * .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."); 
} 

PS: - 这是工作的罚款为 “LS” command.when我使用“* “在任何命令中,一切都不起作用。需要在java中的命令中替换“*”。

UPDATE

感谢您的帮助球员。

现在我需要在java中评论“ls -d1 $ PWD/**”的相同结果。它将用完整路径列出所有的目录名称。

谢谢你的时间。

+0

有没有你需要用'ls'命令来做到这一点任何具体的原因是什么? – 2013-03-24 12:52:58

+1

[**在java中getRuntime.exec()是否理解*或不是?**](http://stackoverflow.com/questions/11789952/does-getruntime-exec-in-java-understands-or-not) – 2013-03-24 12:57:47

+0

是啊...我需要用完整路径打印文件/目录名称。所以我尝试了这个例子。 – RJSV 2013-03-24 12:59:51

回答

11

您可能会发现这更可靠:

Path dir = Paths.get("/path/to/directory"); 

try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.c")) { 
    for (Path file : stream) { 
     // Do stuff with file 
    } 
} 
+0

+1:从Java 7开始,“文件”应该是最好的选择。 – millimoose 2013-03-24 16:32:04

1

*由shell扩展。因此,要使用它扩展一个文件名作为glob,你必须通过一个shell调用ls命令,例如,像这样:

String lscmd = " bash -c 'ls *.c' "; 

编辑

从@Stephen好点,约EXEC不能分裂命令。要执行ls -d1 $PWD/*,你可以做这样的:

String lscmd = "ls -d1 $PWD/*"; 
Process p = Runtime.getRuntime().exec(new String[]{"bash", "-c", lscmd}); 
-1

我知道,它并不直接回答你的问题,但是如果可以的话,更喜欢独立于操作系统的方式在目录中列出文件。

的优点是 - 不是Unix/Linux的具体 不需要产卵过程(它非常昂贵)。

在这里你可以找到如何做到这一点在Java中的例子:

List Files in Directory

希望这有助于

+0

只发布链接到(可能提供)答案的链接被认为是不好的做法。这应该是一个评论,或者扩展到包含解决方案体内OPs问题的实际解决方案的代码。 – millimoose 2013-03-24 16:31:09

1

Java的方法是使用一个FilenameFilter。我适应一个代码示例从here

import java.io.File; 
import java.io.FilenameFilter; 
public class Filter implements FilenameFilter { 

    protected String pattern; 

    public Filter (String str) { 
    pattern = str; 
    } 

    public boolean accept (File dir, String name) { 
    return name.toLowerCase().endsWith(pattern.toLowerCase()); 
    } 

    public static void main (String args[]) { 

    Filter nf = new Filter (".c"); 

    // current directory 
    File dir = new File ("."); 
    String[] strs = dir.list(nf); 

    for (int i = 0; i < strs.length; i++) { 
     System.out.println (strs[i]); 
    } 
    } 
} 

更新:

为您更新,你可以对每个文件遍历一个new File (".").listFiles();并发出file.getAbsolutePath()

1

您需要执行的形式“的bash -c 'LS的* .c' 的命令“...因为java exec方法不明白'*'扩展(globbing)。 (“bash -c”形式确保shell用于处理命令行)

但是,不能简单地将上述命令作为单个字符串提供给Runtime.exec(...),因为exec也不理解正确的方法来分割引用它的acommand字符串。

因此,为了获得exec做正确的事情,你需要做这样的事情:

String lscmd = "ls *.c"; 
    Process p = Runtime.getRuntime().exec(new String[]{"bash", "-c", lscmd}); 

换句话说,你需要做手工的说法分裂...


应该注意的是,这个例子也可以通过使用FileMatcher执行“通配”并从结果中组装参数列表来实现。但是,如果你正在运行除了运行ls ......和IMO的复杂性是不合理的,那么这很复杂。

+0

这在某种程度上是不准确的,Java中存在一个globbing实现:http://docs.oracle.com/javase/tutorial/essential/io/find.html(使用shell仍然是获取一致行为的有效选项。) – millimoose 2013-03-24 16:29:32

+1

@millimoose - 你是对的。更新。 – 2013-03-24 22:52:55

1

一个例子,列出文件,因为Java 7中

import java.io.IOException; 
import java.nio.file.DirectoryStream; 
import java.nio.file.Files; 
import java.nio.file.Path; 
import java.nio.file.Paths; 
public class Main { 
    public static void main(String[] args) { 
     Path dir = Paths.get("/your/path/"); 
     try { 
      DirectoryStream<Path> ds = Files.newDirectoryStream(dir, "*.{c}"); 
      for (Path entry: ds) { 
       System.out.println(entry); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
}