File extension and check for existence
I need to check if a file exists. This can be done with the method File#exists()
. But this existence check is case sensitive. I mean if I have the filename some_image_file.jpg in the code, but if physically the file is some_image_file.JPG then this method says the file doesn't exist. How can I check for the existence of an extension insensitive file and get the actual filename?
In my scenario, I have an excel file. Each line contains metadata for the files and a filename. In some cases, I only have the filename, or other cases where I might have the full path. I designate a string as a document.
These files are hosted on the server. My task -
- Read the excel file line by line and list all the documents.
- Take out the file name or file path.
- Create a fully qualified path to the file.
- Check if the file exists.
- Confirm other metadata / information provided in the document.
- Download the file.
My application throws an exception in case the file does not exist or some metadata is invalid.
The excel file is written by a client and they spelled the filename incorrectly, I mean if the file physically has a lowercase extension, they wrote an uppercase extension, the opposite is also true.
I am running an application on a unix server.
Since the file extensions do not match, so File#exists()
gives false and my code eventually throws an exception.
The folders that contain the files can have 30,000 or more files.
I want to
- To execute the full path to the file.
- Check if the file exists.
- If it doesn't exist, then
- Check for file existence by converting the extension case.
- If it doesn't exist after case conversion, then throw an exception.
- If it exists, return the actual file name or file path.
If the file name has a file extension similar to .Jpg , I don't know what to do! Should I check it by rearranging it, changing case?
source to share
This is how I solved the problem:
public String getActualFilePath() {
File givenFile = new File(filePath);
File directory = givenFile.getParentFile();
if(directory == null || !directory.isDirectory()) {
return filePath;
}
File[] files = directory.listFiles();
Map<String, String> fileMap = new HashMap<String, String>();
for(File file : files) {
if(file.isDirectory()){
continue;
}
String absolutePath = file.getAbsolutePath();
fileMap.put(absolutePath, StringUtils.upperCase(absolutePath));
}
int noOfOcc = 0;
String actualFilePath = "";
for(Entry<String, String> entry : fileMap.entrySet()) {
if(filePath.toUpperCase().equals(entry.getValue())) {
actualFilePath = entry.getKey();
noOfOcc++;
}
}
if(noOfOcc == 1) {
return actualFilePath;
}
return filePath;
}
Here filePath
is the full path to the file.
source to share
You can get the filenames in the folder with
File.list()
and check the names with
equalsIgnoreCase()
Or try http://commons.apache.org/io/ and use
FileNameUtils.directoryContains(final String canonicalParent, final String canonicalChild)
source to share
Canonical name returns a case-sensitive name. If it returns a different string than the file name you are looking for, the file exists otherwise.
So check if the file exists or its canonical name is different
public static boolean fileExistsCaseInsensitive(String path) {
try {
File file = new File(path);
return file.exists() || !file.getCanonicalFile().getName().equals(file.getName());
} catch (IOException e) {
return false;
}
}
source to share