不区分大小写的File.equals,区分大小写的文件系统 [英] Case-insensitive File.equals on case-sensitive file system

查看:275
本文介绍了不区分大小写的File.equals,区分大小写的文件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串形式的文件路径。在Java中,我需要确定文件系统上是否存在该文件(我们的代码需要跨平台,因为它在Windows,Linux和OS X上运行)。



<问题是,文件路径和文件本身的情况可能不匹配,即使它们代表相同的文件(大概这是因为它们起源于Windows,并且没有注意到这种差异)。

b
$ b

例如,我有一个ABC.txt的文件路径。文件系统上存在名为abc.txt的文件。以下代码将在Windows上返回 true ,但在Linux上返回 false

  new File(ABC.txt)。exists(); 

什么是确定文件是否存在的最好方法,是否存在以返回句柄文件系统上的文件?

解决方案

从目录( File.list() )并使用 equalsIgnoreCase()比较名称。


I have a file path in String form. In Java, I need to determine if that file exists on the file system (and our code needs to be cross-platform as it runs on Windows, Linux and OS X).

The problem is that the case of the file path and the file itself may not match, even though they do represent the same file (presumably this is because they originated on Windows and the discrepancy was not noticed).

For example, I have a file path of "ABC.txt". A file called "abc.txt" exists on the file system. The following code will return true on Windows but false on Linux:

new File("ABC.txt").exists();

What is the best way to determine if the file exists, and if it exists to return a handle to the file on the file system?

解决方案

Get the list of files from the directory (File.list()) and compare the names using equalsIgnoreCase().

这篇关于不区分大小写的File.equals,区分大小写的文件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆