带有通配符输入的unix'ls'命令 - 在java 6中等效

时间:2013-03-27 12:01:05

标签: java unix wildcard ls

我需要在Unix cli中使用此命令在java中的等效行为:

ls /data/archive/users/*/*.xml

哪个输出我:

/data/archive/users/2012/user1.xml
/data/archive/users/2013/user2.xml

Java 6是否有简单的等效实现?

4 个答案:

答案 0 :(得分:2)

使用java.util.Scanner获取用户输入并使用java.io.File.listFiles(FilenameFilter)方法获取具有特定过滤器的文件夹中的文件列表。

答案 1 :(得分:0)

是的,有,它被称为list类的File方法。有关详细信息,请参阅Javadoc

答案 2 :(得分:0)

我忘了这来自哪里,但这应该是一个好的开始。还有更多可通过Google获得。

public class RegexFilenameFilter implements FilenameFilter {
  /**
   * Only file name that match this regex are accepted by this filter
   */
  String regex = null; // setting the filter regex to null causes any name to be accepted (same as ".*")

  public RegexFilenameFilter() {
  }

  public RegexFilenameFilter(String filter) {
    setWildcard(filter);
  }

  /**
   * Set the filter from a wildcard expression as known from the windows command line
   * ("?" = "any character", "*" = zero or more occurances of any character")
   *
   * @param sWild the wildcard pattern
   *
   * @return this
   */
  public RegexFilenameFilter setWildcard(String sWild) {
    regex = wildcardToRegex(sWild);

    // throw PatternSyntaxException if the pattern is not valid
    // this should never happen if wildcardToRegex works as intended,
    // so thiw method does not declare PatternSyntaxException to be thrown
    Pattern.compile(regex);
    return this;
  }

  /**
   * Set the regular expression of the filter
   *
   * @param regex the regular expression of the filter
   *
   * @return this
   */
  public RegexFilenameFilter setRegex(String regex) throws java.util.regex.PatternSyntaxException {
    this.regex = regex;
    // throw PatternSyntaxException if the pattern is not valid
    Pattern.compile(regex);

    return this;
  }

  /**
   * Tests if a specified file should be included in a file list.
   *
   * @param dir the directory in which the file was found.
   *
   * @param name the name of the file.
   *
   * @return true if and only if the name should be included in the file list; false otherwise.
   */
  public boolean accept(File dir, String name) {
    boolean bAccept = false;

    if (regex == null) {
      bAccept = true;
    } else {
      bAccept = name.toLowerCase().matches(regex);
    }

    return bAccept;
  }

  /**
   * Converts a windows wildcard pattern to a regex pattern
   *
   * @param wild - Wildcard patter containing * and ?
   *
   * @return - a regex pattern that is equivalent to the windows wildcard pattern
   */
  private static String wildcardToRegex(String wild) {
    if (wild == null) {
      return null;
    }
    StringBuilder buffer = new StringBuilder();

    char[] chars = wild.toLowerCase().toCharArray();

    for (int i = 0; i < chars.length; ++i) {
      if (chars[i] == '*') {
        buffer.append(".*");
      } else if (chars[i] == '?') {
        buffer.append('.');
      } else if (chars[i] == ';') {
        buffer.append('|');
      } else if ("+()^$.{}[]|\\".indexOf(chars[i]) != -1) {
        buffer.append('\\').append(chars[i]); // prefix all metacharacters with backslash
      } else {
        buffer.append(chars[i]);
      }
    }

    return buffer.toString();
  }
}

答案 3 :(得分:0)

这是我使用的代码,它适用于相对路径和绝对路径:

DirectoryScanner scanner = new DirectoryScanner();  
if (!inputPath.startsWith("/") || inputPath.startsWith(".")) {  
    scanner.setBasedir(".");  
}  
scanner.setIncludes(new String[]{inputPath});  
scanner.setCaseSensitive(false);  
scanner.scan();  
String[] foundFiles = scanner.getIncludedFiles();

(来自org.apache.tools.ant的DirectoryScanner)