Check if file is in (sub)directory

前端 未结 8 1028
醉梦人生
醉梦人生 2021-01-04 05:50

I would like to check whether an existing file is in a specific directory or a subdirectory of that.

I have two File objects.

File dir;
File file;
<         


        
8条回答
  •  猫巷女王i
    2021-01-04 06:27

    I would create a small utility method:

    public static boolean isInSubDirectory(File dir, File file) {
    
        if (file == null)
            return false;
    
        if (file.equals(dir))
            return true;
    
        return isInSubDirectory(dir, file.getParentFile());
    }
    

提交回复
热议问题