简体   繁体   English

如何确保区分大小写的资源是我在不区分大小写的文件系统上寻找的资源?

[英]How to ensure case-sensitive resource is what I look for on a case-insensitive file system?

I have a program which needs to lookup resources using ClassLoader#getResource(String) . 我有一个程序需要使用ClassLoader#getResource(String)查找资源。 The program normally works fine but on a Windows machine I'm running into a problem. 该程序通常可以正常运行,但是在Windows计算机上我遇到了问题。 I broke it down to property of the file system being case-insensitive. 我将其分解为不区分大小写的文件系统属性。 Normally, several paths are tried to look for the correct file. 通常, 尝试使用几种路径来查找正确的文件。 On a case-insensitive system, however, it is possible the wrong file is matched . 但是,在不区分大小写的系统上,可能会匹配错误的文件 In my program, resource names are case-sensitive , so the returned file on a case-insensitive system can be wrong if it has different capitalization. 在我的程序中,资源名称区分大小写 ,因此如果大小写不同,则不区分大小写的系统上返回的文件可能是错误的。

For example: I am looking for bar/Foo.class and get back a file bar/FOO.class. 例如:我正在寻找bar / Foo.class并返回文件bar / FOO.class。 I need to recognize this case and reject it . 我需要认清这种情况并予以拒绝

What is the best way to check if the returned URL fits my specified path in a case-sensitive manner? 以区分大小写的方式检查返回的URL是否适合我指定的路径的最佳方法是什么?

I was thinking about comparing the specified path with the result of getPath of the URL. 我正在考虑将指定的路径与URL的getPath结果进行比较。 But nothing anywhere tells me if the resulting URL will use my given path or the real path. 但是任何地方都无法告诉我结果URL是否将使用我给定的路径或实际路径。 Thus, unless someone shows me documentation that this is different and specified, I cannot be sure this will always be the case. 因此,除非有人向我显示不同且指定的文档,否则我无法确定情况总是如此。

The problem is the fact you're not using absolute paths to find a certain resource. 问题是您没有使用绝对路径来查找特定资源。 Therefor, the answer to your question is simply you can't . 因此,您的问题的答案就是您不能 To a case insensitive system, there can only be one file at the path "bar/foo.class" from any one context . 对于不区分大小写的系统, 任何上下文中的路径“ bar / foo.class”只能有一个文件。 The only way there can be multiple files at such a path is if they have different absolute paths (eg /a/bar/foo.class and /b/bar/foo.class), in which case they have a different context (=there are two different folders "bar" which both contain a file named "foo.class"). 在这样的路径上可以有多个文件的唯一方法是,如果它们具有不同的绝对路径(例如/a/bar/foo.class和/b/bar/foo.class),在这种情况下它们具有不同的上下文 (=有两个不同的文件夹“ bar”,都包含一个名为“ foo.class”的文件)。

When searching all resources matching a non-absolute path (like bar/foo.class), there is absolutely no guarantee there will only be one file on the system that matches that path (matches meaning the file's absolute path contains the path you specified). 搜索与非绝对路径匹配的所有资源时(例如bar / foo.class),绝对不能保证系统上只有一个文件与该路径匹配(匹配表示文件的绝对路径包含您指定的路径) 。 Further more, if there are multiple matches, there is no guarantee that they will differ in capitalization, so using case sensitivity to verify which file is wrong is not a good approach. 此外,如果存在多个匹配项,则不能保证它们的大小写会有所不同,因此使用区分大小写的方法来验证哪个文件是错误的不是一个好方法。 Moreover, to a case insensitive system both files have the same name but are just at a different absolute location (but the same relative location to a different context [read: folder]). 此外,对于不区分大小写的系统,两个文件都具有相同的名称,但是它们位于不同的绝对位置(但是相对于不同上下文的相同相对位置[read:文件夹])。

If possible, search for files using their absolute path. 如果可能,使用文件的绝对路径搜索文件。 That way, only the file with identical capitalization will be matched on case sensitive systems and the one file (if there is one) at that absolute path will be matched on case insensitive systems. 这样,只有大小写相同的文件才能在区分大小写的系统上匹配,而在该绝对路径上的一个文件(如果有)将在不区分大小写的系统上匹配。 I hope this answer is more useful to you. 我希望这个答案对您更有用。

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

相关问题 获取文件区分大小写的名称,不区分大小写拼写 - Get file case-sensitive name, with case-insensitive spelling 区分大小写的文件系统上的不区分大小写的File.equals - Case-insensitive File.equals on case-sensitive file system 如何检测文件系统是否区分大小写? - How do I detect whether the file system is case-sensitive? 当java(解释器)区分大小写时,为什么Java编译器(javac)不区分大小写? - Why is the Java compiler (javac) case-insensitive when java (the interpreter) is case-sensitive? 如何使收件人的名称对邮件系统不区分大小写? - How to make the names of the recipients case-insensitive for a mail system? 如何在 Linux 系统上按字典顺序(不区分大小写)对文件\目录树进行排序 - How to sort file\directory tree in lexicographic order (case-insensitive) on Linux system 区分大小写的文件名处理 - case-sensitive processing of file names 如何使用TortoiseGit提交区分大小写的重命名文件? - How do I do commit a renamed file with case-sensitive using TortoiseGit? 语言环境-如何获取区分大小写的值? - Locale - how to get case-sensitive value? JAVA //如何使它不区分大小写? - JAVA // How do i make it case-insensitive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM