如何使用Java Glob模式搜索(区分大小写)文件? [英] How to search (case-sensitive) for files using Java glob pattern?

查看:81
本文介绍了如何使用Java Glob模式搜索(区分大小写)文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在检查getPathMatcher 方法> FileSystem 类.该方法的文档说:

I am checking the getPathMatcher method of FileSystem class. The documentation of the method says:

对于glob和regex语法,匹配的详细信息,例如匹配是否区分大小写,取决于实现因此未指定.

For both the glob and regex syntaxes, the matching details, such as whether the matching is case sensitive, are implementation-dependent and therefore not specified.

我对此进行了测试,并且知道默认情况下它不区分大小写.如何使其区分大小写?

I tested this and came to know that by default it is case-insensitive. How to make it case-sensitive?

我正在Windows7上使用JDK7u25.

I am using JDK7u25 on Windows7.

推荐答案

否,默认情况下不区分大小写.正如文档所说,区分大小写取决于实现.

No, it is not case-insensitive by default. As the doc says, case sensitivity is implementation dependent.

NTFS是保留大小写,但不区分大小写.也就是说,名为 README.txt 的文件将保留大小写(保留大小写);但是尝试以名称 Readme.TXT 查找它,例如有效(不区分大小写).

And NTFS is case preserving but case insensitive. That is, a file named README.txt will keep its case (case preserving); but trying and finding it by the name Readme.TXT, say, will work (case insensitive).

在文件系统 区分大小写的Unix系统上不是这种情况.

This is not the case on Unix systems, whose filesystems are case sensitive.

不幸的是,没有办法解决!除了创建自己的 Filesystem 实现(包装默认值并使其区分大小写)之外.

Unfortunately, there is no way around that! Other than creating your own Filesystem implementation wrapping the default and make it case sensitive.

这是一个用途非常有限的 FileSystem 的示例,它将能够生成文件名扩展名的区分大小写的匹配":

Here is an example of a VERY limited purpose FileSystem which will be able to generate a "case sensitive matching" of filename extensions:

public final class CaseSensitiveNTFSFileSystem
    extends FileSystem
{
    private static final Pattern MYSYNTAX = Pattern.compile("glob:\\*(\\..*)");

    private final FileSystem fs;

    // "fs" is the "genuine" FileSystem provided by the JVM
    public CaseSensitiveNTFSFileSystem(final FileSystem fs)
    {
        this.fs = fs;
    }

    @Override
    public PathMatcher getPathMatcher(final String syntaxAndPattern)
    {
        final Matcher matcher = MYSYNTAX.matcher(syntaxAndPattern);
        if (!matcher.matches())
            throw new UnsupportedOperationException();
        final String suffix = matcher.group(1);
        final PathMatcher orig = fs.getPathMatcher(syntaxAndPattern);

        return new PathMatcher()
        {
            @Override
            public boolean matches(final Path path)
            {
                return orig.matches(path)
                    && path.getFileName().endsWith(suffix);
            }
        };
    }

    // Delegate all other methods of FileSystem to "fs"
}

这篇关于如何使用Java Glob模式搜索(区分大小写)文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆