如何检测文件系统是否区分大小写?

发布于 2024-08-02 21:57:29 字数 321 浏览 5 评论 0原文

我有一个来自文件夹的文件名的 List 以及某个文件名 String。我想检测文件名是否在列表中,但需要尊重底层文件系统是否区分大小写的属性。

有没有什么简单的方法可以做到这一点(除了检查 System.getProperty("os.name", "").toLowerCase().indexOf("windows")!=-1System.getProperty("os.name", "").toLowerCase().indexOf("windows")!=-1)? ;-)

I have a List<String> of file names from a folder and a certain file name as String. I want to detect whether the file name is in the list, but need to respect the underlying file system's property of whether it is case-sensitive.

Is there any easy way to do this (other than the "hack" of checking System.getProperty("os.name", "").toLowerCase().indexOf("windows")!=-1)? ;-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

微凉徒眸意 2024-08-09 21:57:29

不要使用字符串来表示您的文件;使用 java.io.File:

http://java.sun.com/javase/6/docs/api/java/io/File.html#equals(java.lang.Object)

Don't use Strings to represent your files; use java.io.File:

http://java.sun.com/javase/6/docs/api/java/io/File.html#equals(java.lang.Object)

零時差 2024-08-09 21:57:29
boolean isFileSystemCaseSensitive = !new File( "a" ).equals( new File( "A" ) );
boolean isFileSystemCaseSensitive = !new File( "a" ).equals( new File( "A" ) );
葬花如无物 2024-08-09 21:57:29

看起来您可以使用 IOCase

It looks like you can use the IOCase.

如日中天 2024-08-09 21:57:29

编写一个名为“HelloWorld”的文件;尝试读取名为“hELLOwORLD”的文件?

Write a file named "HelloWorld"; attempt to read a file named "hELLOwORLD"?

葬花如无物 2024-08-09 21:57:29

我认为现有的任何示例都无法正确处理此问题,您需要将文件写入磁盘。

    private boolean caseSensitivityCheck() {
    try {
        File currentWorkingDir = new File(System.getProperty("user.dir"));
        File case1 = new File(currentWorkingDir, "case1");
        File case2 = new File(currentWorkingDir, "Case1");
        case1.createNewFile();
        if (case2.createNewFile()) {
            System.out.println("caseSensitivityCheck: FileSystem of working directory is case sensitive");
            case1.delete();
            case2.delete();
            return true;
        } else {
            System.out.println("caseSensitivityCheck: FileSystem of working directory is NOT case sensitive");
            case1.delete();
            return false;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

I don't think any of the existing examples actually handle this properly, you need to write the file to disk.

    private boolean caseSensitivityCheck() {
    try {
        File currentWorkingDir = new File(System.getProperty("user.dir"));
        File case1 = new File(currentWorkingDir, "case1");
        File case2 = new File(currentWorkingDir, "Case1");
        case1.createNewFile();
        if (case2.createNewFile()) {
            System.out.println("caseSensitivityCheck: FileSystem of working directory is case sensitive");
            case1.delete();
            case2.delete();
            return true;
        } else {
            System.out.println("caseSensitivityCheck: FileSystem of working directory is NOT case sensitive");
            case1.delete();
            return false;
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
衣神在巴黎 2024-08-09 21:57:29

您可以使用以下静态方法:

/**
 * <p>
 * Checks, whether the provided path points to a case-sensitive file system or not.
 * </p>
 * <p>
 * It does so by creating two temp-files in the provided  path and comparing them. Note that you need to have write
 * access to the provided path.
 *
 * @param pathToFileSystem path to the file system to test
 * @param tmpFileName      name of the temp file that is created
 * @return {@code true}, if the provided path points to a case-sensitive file system; {@code false} otherwise
 * @throws IOException if IO operation fails
 */
public static boolean fileSystemIsCaseSensitive(Path pathToFileSystem, String tmpFileName) throws IOException {

    Path a = Files.createFile(pathToFileSystem.resolve(Paths.get(tmpFileName.toLowerCase())));
    Path b = null;
    try {
        b = Files.createFile(pathToFileSystem.resolve(Paths.get(tmpFileName.toUpperCase())));
    } catch (FileAlreadyExistsException e) {
        return false;
    } finally {
        Files.deleteIfExists(a);
        if (b != null) Files.deleteIfExists(b);
    }
    return true;
}

按如下方式使用:

@Test
void fileSystemIsCaseSensitive01() throws IOException {
    assertTrue(Util.fileSystemIsCaseSensitive(Paths.get("/Volumes/Case-sensitive-Volume/Data"), "a"));
}

@Test
void fileSystemIsCaseSensitive02() throws IOException {
    assertFalse(Util.fileSystemIsCaseSensitive(Paths.get("/Volumes/Case-insensitive-Volume/Data"), "a"));
}

注意提供的不同卷(Case-sensitive-Volume 与 Case-insensitve-Volume)。

You can use the following static method:

/**
 * <p>
 * Checks, whether the provided path points to a case-sensitive file system or not.
 * </p>
 * <p>
 * It does so by creating two temp-files in the provided  path and comparing them. Note that you need to have write
 * access to the provided path.
 *
 * @param pathToFileSystem path to the file system to test
 * @param tmpFileName      name of the temp file that is created
 * @return {@code true}, if the provided path points to a case-sensitive file system; {@code false} otherwise
 * @throws IOException if IO operation fails
 */
public static boolean fileSystemIsCaseSensitive(Path pathToFileSystem, String tmpFileName) throws IOException {

    Path a = Files.createFile(pathToFileSystem.resolve(Paths.get(tmpFileName.toLowerCase())));
    Path b = null;
    try {
        b = Files.createFile(pathToFileSystem.resolve(Paths.get(tmpFileName.toUpperCase())));
    } catch (FileAlreadyExistsException e) {
        return false;
    } finally {
        Files.deleteIfExists(a);
        if (b != null) Files.deleteIfExists(b);
    }
    return true;
}

Use it as follows:

@Test
void fileSystemIsCaseSensitive01() throws IOException {
    assertTrue(Util.fileSystemIsCaseSensitive(Paths.get("/Volumes/Case-sensitive-Volume/Data"), "a"));
}

@Test
void fileSystemIsCaseSensitive02() throws IOException {
    assertFalse(Util.fileSystemIsCaseSensitive(Paths.get("/Volumes/Case-insensitive-Volume/Data"), "a"));
}

Note the different volumes provided (Case-sensitive-Volume vs. Case-insensitve-Volume).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文