Case sensitive file extension and existence checking

吃可爱长大的小学妹 提交于 2019-12-02 10:14:28

问题


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?


回答1:


You could get the file names in a folder with

File.list() 

and check names by means of

equalsIgnoreCase()

Or try http://commons.apache.org/io/ and use

FileNameUtils.directoryContains(final String canonicalParent, final String canonicalChild)



回答2:


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.




回答3:


The canonical name returns the name with case sensitive. If it returns a different string than the name of the file you are looking for, the file exists with a different case.

So, test if the file exists or if 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;
    }
}


来源:https://stackoverflow.com/questions/13620555/case-sensitive-file-extension-and-existence-checking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!