我有一個列表Path
(java.nio.file
)。Deduce最長的路徑
例如
/a/b/c/d
/a/b/c
/a/b/
/a/x/y/z
/a/x/
在這些路徑我只需要檢索最長的路徑。例如, /a/b/c/d
和/a/x/y/z
是最長的路徑。
我應該如何檢索通過使用或不使用Java中的路徑接口中的任何方法?
我有一個列表Path
(java.nio.file
)。Deduce最長的路徑
例如
/a/b/c/d
/a/b/c
/a/b/
/a/x/y/z
/a/x/
在這些路徑我只需要檢索最長的路徑。例如, /a/b/c/d
和/a/x/y/z
是最長的路徑。
我應該如何檢索通過使用或不使用Java中的路徑接口中的任何方法?
試試這個代碼:
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathTest {
public static void main(String[] args) {
String path = "https://stackoverflow.com/a/b/c/d";
Path p = Paths.get(path);
int num = p.getNameCount();
System.out.println(num); // prints 4
}
}
是否可以獲取葉節點? – Arun 2015-03-31 14:36:43
使用'p.getName(num-1)'或'p.getName(p.getNameCount() - 1)' – 2015-03-31 14:45:21
path.getNameCount()可能?請參閱http://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html#getNameCount%28%29 – BretC 2015-03-31 14:21:53