Case sensitive file extension and existence checking

后端 未结 3 694
遥遥无期
遥遥无期 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:33

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

提交回复
热议问题