繁体   English   中英

获取文件区分大小写的名称,不区分大小写拼写

[英]Get file case-sensitive name, with case-insensitive spelling

我正在创建一个用户从中选择文件的应用程序:

FilePicker.PickFile(filename)

其中filename是一个字符串。

在该方法中,它将转换为:

File file = new File(filename);

这并没有错。 接下来,我做,

if(file.exists()){
    System.out.println(file.getName());
}
else{
    System.out.println("Fail.");
}

这就是问题的开始。 我想获取文件的名称,例如“HELLO.txt”,但如果filename是“hello.txt”,它仍然传递file.exists()检查, file.getName()返回为“hello.txt” ,“不是”HELLO.txt“。 有没有办法,将file.getName()作为区分大小写的版本返回为“HELLO.txt?” 谢谢!

一个例子:

HELLO.txt is the real file

FilePicker.PickFile("hello.txt");

OUTPUT:

hello.txt

当您使用Windows(保留大小写(FAT32 / NTFS / ..))时,可以使用file.getCanonicalFile() .getName()来获取所选文件的规范名称。

当您使用Linux或Android并且想要根据不一定匹配大小写的文件名选择文件时,遍历文件目录( file.getParent() )中的所有文件,并选择.equalsIgnoreCase filename 或者在区分大小写的文件系统上查看不区分大小写的File.equals

/**
 * Maps lower case strings to their case insensitive File
 */
private static final Map<String, File> insensitiveFileHandlerCache = new HashMap<String, File>();

/**
 * Case insensitive file handler. Cannot return <code>null</code>
 */
public static File newFile(String path) {
    if (path == null)
        return new File(path);
    path = path.toLowerCase();
    // First see if it is cached
    if (insensitiveFileHandlerCache.containsKey(path)) {
        return insensitiveFileHandlerCache.get(path);
    } else {
        // If it is not cached, cache it (the path is lower case)
        File file = new File(path);
        insensitiveFileHandlerCache.put(path, file);

        // If the file does not exist, look for the real path
        if (!file.exists()) {

            // get the directory
            String parentPath = file.getParent();
            if (parentPath == null) {
                // No parent directory? -> Just return the file since we can't find the real path
                return file;
            }

            // Find the real path of the parent directory recursively
            File dir = Util.newFile(parentPath);

            File[] files = dir.listFiles();
            if (files == null) {
                // If it is not a directory
                insensitiveFileHandlerCache.put(path, file);
                return file;
            }

            // Loop through the directory and put everything you find into the cache
            for (File otherFile : files) {
                // the path of our file will be updated at this point
                insensitiveFileHandlerCache.put(otherFile.getPath().toLowerCase(), otherFile);
            }

            // if you found what was needed, return it
            if (insensitiveFileHandlerCache.containsKey(path)) {
                return insensitiveFileHandlerCache.get(path);
            } 
        }
        // Did not find it? Return the file with the original path
        return file;
    }
}

使用

File file = newFile(path);

代替

File file = new File(path);

它由缓存支持,所以它不应该太慢。 做了一些试运行,它似乎工作。 它以递归方式检查父目录,看它们是否有正确的字母大小写。 然后它为每个目录列出所有文件并缓存其正确的字母大小写。 最后,它检查是否找到了包含路径的文件,并返回带有区分大小写路径的文件。

在Windows上的Java 7及更高版本中,您可以使用Path#toRealPath(NOFOLLOW_LINKS),并且在存在符号链接的情况下,它会比getCanonicalFile()更正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM