views:

308

answers:

3

I have a List<String> of file names from a folder and a certain file name as String. I want to detect whether the file name is in the list, but need to respect the underlying file system's property of whether it is case-sensitive.

Is there any easy way to do this (other than the "hack" of checking System.getProperty("os.name", "").toLowerCase().indexOf("windows")!=-1)? ;-)

+1  A: 

Write a file named "HelloWorld"; attempt to read a file named "hELLOwORLD"?

Roger Lipscombe
+11  A: 

Don't use Strings to represent your files; use java.io.File:

http://java.sun.com/javase/6/docs/api/java/io/File.html#equals%28java.lang.Object%29

Steve M
yeah this is the most elegant and stable solution I think...
Epaga
As an additional note, you can get an array of files from a directory using File's listFiles() method on a File object for said directory. This can then be manipulated as an array or converted to a list using Arrays.asList
R. Bemrose
yup basically what i did was change the list() method to a listFiles method...
Epaga
A: 

boolean isFileSystemCaseSensitive = !new File( "a" ).equals( new File( "A" ) );

Alexander