在 Linux 上运行 Java 时,如何避免因区分大小写而出现 FileNotFound 异常?

发布于 2024-08-11 07:30:14 字数 270 浏览 7 评论 0原文

我的 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 技术交流群。

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

发布评论

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

评论(7

雨后咖啡店 2024-08-18 07:30:14

修复它!

从长远来看,你设计的任何规避修复的方案都会更糟糕。

Fix it!

Any scheme you devise to circumvent fixing it will be worse in the long run.

南渊 2024-08-18 07:30:14

无法避免这种情况,因为 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 load Abc.txt when the filename is abc.txt. This is a bad habit (taught by spending too much time on Windows) rather than a Linux/Unix problem.

聽兲甴掵 2024-08-18 07:30:14

好吧,首先我认为您应该考虑采用一致的命名方案,而不是使用某种解决方法。

无论如何,读取所有文件名并将它们放入包含小写名称作为键的映射中怎么样?然后您可以从地图中查找正确的文件名

这还可以让您检测到冲突,例如同一目录中的两个文件“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).

囚我心虐我身 2024-08-18 07:30:14

假设 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.

柏拉图鍀咏恒 2024-08-18 07:30:14

为什么不能更改大量文件?如果文件数量确实是唯一阻碍您的因素,那么只需编写一个小脚本将它们全部重命名为小写即可。

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.

毁我热情 2024-08-18 07:30:14

从您的问题中尚不清楚是什么导致您的文件发生大小写更改。如果所有文件在 Linux 中都是小写,而在 Windows 上是混合大小写,则可以将文件名转换为小写,如下所示:

new File(filename.toLowerCase())

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 File(filename.toLowerCase())
╄→承喏 2024-08-18 07:30:14

有一个解决方案,运行时性能很糟糕,但实现起来非常简单:

的内容

openFile(name);

public FileReader openFile(String name) throws FileNotFoundException {
  File dir = (new File(name)).getParentFile();
  for (File f : dir.listFiles()) {
    if (f.getName().equalsIgnoreCase(name)) {
    return new FileReader(f);
  }
  throw new FileNotFoundException("File not found: " + name);
}

new FileReader(name) 替换为“我还没有编译此代码,它可能有拼写错误和错误”之类 。我把它们留给你来修复。

There's a solution that has horrible runtime performance but is very simple to implement:

Replace new FileReader(name) with something like

openFile(name);

public FileReader openFile(String name) throws FileNotFoundException {
  File dir = (new File(name)).getParentFile();
  for (File f : dir.listFiles()) {
    if (f.getName().equalsIgnoreCase(name)) {
    return new FileReader(f);
  }
  throw new FileNotFoundException("File not found: " + name);
}

I haven't compiled this code, it may have typos and bugs. I leave them to you to fix.

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