Case sensitive file extension and existence checking

后端 未结 3 723
遥遥无期
遥遥无期 2021-01-25 08:55

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 f

3条回答
  •  悲哀的现实
    2021-01-25 09:17

    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 fileMap = new HashMap();
    
        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 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.

提交回复
热议问题