Java:检查路径是否为文件的父级 [英] Java: Check if path is parent of a file

查看:240
本文介绍了Java:检查路径是否为文件的父级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到解决方法.

I cant seem to find a solution.

我有两条路径,例如:

D:/Views/me/a.bD:/Views/me/a

D:/Views/me/a.bD:/Views/me/a.b/x/y

我必须确保一个文件/目录不是另一个文件/目录的子目录. 我已经尝试过Contains,但是在这种情况下对我不起作用?

I have to ensure, that one file/directory is not a child of the other. I have tried Contains but it does not work for me in this case?

推荐答案

尝试使用String的startsWith api:

Try using startsWith api of String:

String str1 = "D:/Views/me/a.b";
String str2 = "D:/Views/me/a";
if (str1.startsWith(str2 + ".") || str1.startsWith(str2 + "/")) {
    System.out.println("Yes it is");
}
str1 = "D:/Views/me/a/c/d";
str2 = "D:/Views/me/a";
if (str1.startsWith(str2 + ".") || str1.startsWith(str2 + "/")) {
     System.out.println("Yes it is");
}
Output:
Yes it is
Yes it is

这篇关于Java:检查路径是否为文件的父级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆