在 Linux 上运行 Java 时,如何避免因区分大小写而出现 FileNotFound 异常?
我的 Web 应用程序在 Windows 上运行。我也想在 Linux 上运行我的应用程序。我似乎已经克服了大多数问题,例如路径分隔符等。
现在真正的问题是,当 Java 代码尝试打开文件 Abc.txt
时,我得到 FileNotFoundException
当只有 abc.txt
存在时。 :(
我不能继续将所有文件名更改为小写或大写,因为我有很多文件。在不更改代码的情况下,是否有任何可能的方法来避免这种情况?
My web application runs on Windows. I would like to run my app on Linux also. I seem to have overcome most of the problems such as path separator, etc.
Real problem now is I get FileNotFoundException
when the Java code tries to open a file say Abc.txt
when only abc.txt
exists. :(
I can't go on changing all the filenames to lowercase or uppercase as i have a whole lot of files. Without changing code much is that any possible way to avoid this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
修复它!
从长远来看,你设计的任何规避修复的方案都会更糟糕。
Fix it!
Any scheme you devise to circumvent fixing it will be worse in the long run.
无法避免这种情况,因为
java.io.File
API 依赖于系统。在 Linux/Unix 上操作文件时必须使用正确的大小写。实际上,我的建议/解决方案是在 Windows 上进行开发期间遵循严格且可移植的约定(例如,仅使用小写文件名,或者更好的是,以编程方式访问文件时使用确切的文件名)。老实说,我不明白为什么当文件名是abc.txt
时,您要尝试加载Abc.txt
。这是一个坏习惯(由于在 Windows 上花费太多时间而养成的),而不是 Linux/Unix 的问题。There is no way to avoid this as the
java.io.File
API is system-dependent. You have to use the right case when manipulating files on Linux/Unix. Actually, my advice/solution would be to follow strict and portable conventions during development on Windows (e.g. use lower case filenames only or, better, use the exact file name when accessing it programmatically). To be honest, I don't understand why you are trying to loadAbc.txt
when the filename isabc.txt
. This is a bad habit (taught by spending too much time on Windows) rather than a Linux/Unix problem.好吧,首先我认为您应该考虑采用一致的命名方案,而不是使用某种解决方法。
无论如何,读取所有文件名并将它们放入包含小写名称作为键的映射中怎么样?然后您可以从地图中查找正确的文件名。
这还可以让您检测到冲突,例如同一目录中的两个文件“FileA.txt”和“FILEA.TXT”具有相同的小写表示形式,在这种情况下您知道必须解决该问题一种完全不同的方式(因为你必须知道你想打开哪一个,而且它是不明确的,这样的解决方法就行不通)。
Well, first of all I think you should consider moving to a consistent naming scheme, rather than to use some workaround.
Anyway, what about reading in all file names and putting them into a map that contains the lower-case name as a key? You can then look up the correct file name from the map.
This would also allow you to detect a conflict, e.g. two files "FileA.txt" and "FILEA.TXT" in the same directory that have the same lower-case representations, in which case you know that you have to tackle the problem in a completely different way (because you have to know which one you want to have opened, and it's ambiguous, and such a workaround won't do it then).
假设 Linux 上的文件是混合大小写的,则对此没有简单的答案。
我能想到的最好办法是让您的应用程序列出相关目录并创建实际 Linux 文件名的内存数据结构。然后,要打开不区分大小写的文件,请将路径名拆分为多个组件,使用不区分大小写的搜索来搜索内存树,构建真实的(区分大小写的)路径名并使用它来打开文件。
这样做的问题是,它(实际上是你的应用程序)无法处理同一 Linux 目录中(例如)“foo.txt”和“Foo.txt”的情况。
但最好的解决方案是更改您的应用程序,使其能够使用区分大小写的路径名。
Assuming that the files are mixed case on Linux, there is no simple answer to this.
The best I can think of is to have your application list the relevant directories and create an in memory data structure of the actual Linux filenames. Then to do a case-insensitive file open, you split the pathname into components, search the in-memory tree using case-insensitive search, bulid the real (case-sensitive) pathname and use THAT to do the file open.
The problem with this is that it (and indeed your app) cannot cope with the case where you have (say) "foo.txt" AND "Foo.txt" in the same Linux directory.
But the best solution is to change your application so that it works with case-sensitive pathnames.
为什么不能更改大量文件?如果文件数量确实是唯一阻碍您的因素,那么只需编写一个小脚本将它们全部重命名为小写即可。
Why can't you change a lot of files? If the amount of files is really the only thing that holds you back, then simply write a little script that renames them all to lower-case.
从您的问题中尚不清楚是什么导致您的文件发生大小写更改。如果所有文件在 Linux 中都是小写,而在 Windows 上是混合大小写,则可以将文件名转换为小写,如下所示:
It's not clear from your question what is causing the case change of your files. If all your files are lowercase in linux, while they are mixed case on windows, you could just transform the filename to lower case, like so:
有一个解决方案,运行时性能很糟糕,但实现起来非常简单:
的内容
将
new FileReader(name)
替换为“我还没有编译此代码,它可能有拼写错误和错误”之类 。我把它们留给你来修复。There's a solution that has horrible runtime performance but is very simple to implement:
Replace
new FileReader(name)
with something likeI haven't compiled this code, it may have typos and bugs. I leave them to you to fix.