如何使用带有newDirectoryStream的glob来仅列出没有扩展名的文件?

时间:2014-10-24 19:15:17

标签: java regex glob

此程序列出扩展名为.brd的文件内容,我使用“glob”".brd"作为Files.newDirectoryStream的第二个参数完成。效果很好,并建议Windows文件模式是可以接受的。但没有。

但我有没有扩展名的文件,我也想列出。对于我试过的任何glob模式(Windows文件名模式),我无法得到它。

显然,glob 解释为Windows文件名模式,也不作为正则表达式。

我尝试了各种Windows模式。

- 如果我使用glob“*。”,我应该只获取没有扩展名的文件(这是我想要的),但是我没有文件。

- 如果我使用glob“*。*”,我应该获取所有文件,但我只获得具有扩展名的文件。 (这只是FYI。)(请注意:glob应该是“星点星”,没有空格;格式化问题。)

- 如果我使用glob“*”,我会得到每个文件,这与Windows模式一致。 (FYI)

什么样的glob模式可以获得没有扩展名的JUST文件? 或者,我可以使用什么正则表达式?

package listdirectorycontents;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.nio.file.*;
import java.io.IOException;
import java.util.Scanner;

public class ListDirectoryContents {

  static DirectoryStream<Path>  directoryStream;

  public static void main(String[] args) throws IOException {

    Path path = Paths.get("C:\\Users\\Dov\\My Documents\\boards");

    String output, L;

    directoryStream = Files.newDirectoryStream(path, "*.brd");

      System.out.println("\n" + path + " directory contains:");
      for(Path aFile : directoryStream) {
        output = "";

        try{
          try
            (FileInputStream  inputFile = new FileInputStream(aFile.toString()); 
                      Scanner scanner   = new Scanner(inputFile)
            ) 
          {

            for(int i = 0; i < 4; i++)
              if(scanner.hasNext())
                scanner.next();

            if(scanner.hasNext()){
              L = scanner.next();

              while(L.length() == 4){
                output += L;
                L = scanner.next();
              }
              System.out.println(output);
            }
          }
       }
      catch(FileNotFoundException e){System.out.println("\t*Not a file? " + e);}
    } 
  }
}

globs的文档包括这个有限的帮助(在getPathMatcher下):

A FileSystem implementation supports the "glob" and "regex" syntaxes, and may support others. The value of the syntax component is compared without regard to case. When the syntax is "glob" then the String representation of the path is matched using a limited pattern language that resembles regular expressions but with a simpler syntax. For example...

也许答案是切换到FileVisitor,但我希望尽可能少地重写。

*替代方法*

directoryStream = Files.newDirectoryStream(path);  // to get all files;  no globs
...
for(Path aFile : directoryStream) 
{
  if(aFile.toString().endsWith("brd") || ! aFile.toString().contains("."))
  {
    ...
  }
}

1 个答案:

答案 0 :(得分:1)

Java的globbing引擎与POSIX globbing一致,它与cmd.exe通配符的工作方式略有不同(并且更加理智)。 POSIX globbing与事实是'.'字符在POSIX文件名中并不特殊,这与从DOS继承的Windows传统相反。因此,glob模式中的'*'可以匹配零个或多个字符,包括任意数量的'.'个字符,并且以glob模式显示的'.'必须与'.'匹配在文件名中。顺便提一下,FileSystem.getPathMatcher(String)的API文档涵盖了这一点,特别是:

*.*     Matches file names containing a dot

The * character matches zero or more characters of a name component without crossing directory boundaries.

请注意,'.'字符没有记录特殊处理。

虽然您可以将单个非点字符与glob模式匹配(即"[!.]"),但在glob语法中没有子模式重复运算符。

另一方面,您可以使用正则表达式轻松匹配没有扩展名的文件名。此模式将执行此操作:"[^.]*",假设您使用Matcher.matches()(而不是Matcher.find(),这不需要整个字符串与模式匹配。)