Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Case-insensitive File.equals on case-sensitive file system

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?

like image 776
jwaddell Avatar asked Aug 19 '09 05:08

jwaddell


1 Answers

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

like image 71
Shimi Bandiel Avatar answered Sep 24 '22 18:09

Shimi Bandiel