Search code examples
javafilecase-sensitivecase-insensitivefile-exists

Case sensitive file extension and existence checking


I need to check whether or not a file exists. Which can be accomplished by File#exists() method. But this existence checking is case sensitive. I mean if I have a file name some_image_file.jpg in code but if physically the file is some_image_file.JPG then this method says that the file doesn't exists. How can I check the file existence with case insensitivity to the extension and get the actual file name?

In my scenario, I have a excel file. Each row contains metadata for files and the filename. In some cases I have only the filename or other cases I can have full path. I am denoting a row as a document.

These files are placed in the server. My job is to

  • Read the excel file row by row and list all the documents.
  • Take out the filename or filepath.
  • Create the full path of the file.
  • Check if the file exists or not.
  • Validate other metadata/information provided in the document.
  • Upload the file.

My application throws exception in case the file doesn't exists or if some metadata are invalid.

The excel file is written by the customer and they wrote some file name wrong, I mean if the file physically have the extension in lower case, they have written the extension in upper case, also the converse is true.

I am running the application in unix server.

As the file extensions are not matching so the File#exists() is giving false and eventually my code is throwing exception.

The folders where the files are placed can have 30000 or more files.

What I want is

  • To take the full path of the file.
  • Check if the file exists or not.
  • If it does not exists then
  • Check the file existence by converting the case of the extension.
  • If it doesn't exist after the case conversion then throw exception.
  • If it exists, then return the actual file name or file path.

If the file name has file extension something like .Jpg, don't know what to do! Should I check it by permuting it by changing the case?


Solution

  • This way I had 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.