开发者

Case insensitive directory access in Java on Unix

开发者 https://www.devze.com 2023-03-19 11:38 出处:网络
I am working on Unix, which uses case sensitive filenames and need a way to access them in a case-insensitive manner.

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?


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 ;) )


In Java 7 there is something called a PathMatcher which allows you to use regular expressions to find a file.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消