Reputation: 6054
I am working on Unix, which uses case sensitive filenames and need a way to access them in a case-insensitive manner.
Specifically, at time the program needs to get into a /images directory under a project folder. But because this directory is created manually by the user, it may sometimes be named Images, IMAGES, iMaGes...you get the idea.
Given that there is guaranteed to be only one images directory in any project folder, how can I access this directly without getting the entire directory list and looping through it?
Upvotes: 0
Views: 2414
Reputation: 3264
In Java 7 there is something called a PathMatcher which allows you to use regular expressions to find a file.
Upvotes: 2
Reputation: 114807
There is no other way then reading the full list of directory names and check each and everyone if it matches your pattern.
You can hide it by using a FilenameFilter
that filters all folder names that don't match your pattern (iaw, any folder, whose lowercased name is equal to images
). This may be a better approach, because it will return all candidates (iaw: ./Images
, ./imaGes
, ./IMAGES
if you have creative users that place more then one image folder ;) )
Upvotes: 3
Reputation: 308141
There is no solution that doesn't involve looping over either the list of entries in the directory or the list of possible permutations of lower and upper case letters.
Unix is case sensitive and asking for /image
when only /Image
exists won't get you a positive answer.
The only exception is when the file system that's used is not case sensitive (for example FAT variants are not case-sensitive), but that only really applies for external media, usually.
Upvotes: 1
Reputation: 8734
If it's a folder the user created, then give them a file chooser UI to select the folder. Either that or, when the project is created, create the images folder too in a way that's predictable later.
Upvotes: 0