Check if file is in (sub)directory

前端 未结 8 949
醉梦人生
醉梦人生 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条回答
  •  臣服心动
    2021-01-04 06:25

    You can traverse File Tree starting from your specific DIR. At Java 7, there is Files.walkFileTree method. You have only to write your own visitor to check if current node is searched file. More doc: http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#walkFileTree%28java.nio.file.Path,%20java.util.Set,%20int,%20java.nio.file.FileVisitor%29

提交回复
热议问题