2011-11-22 3 views
53

Java 7은 java.nio.file.Path을 java.io.File 용 possible replacement으로 도입했습니다. 파일로Java 7의 하위 파일/폴더에 액세스하는 방법 java.nio.file.Path?

, 내가 특정 아래의 파일을 액세스 할 때, 내가 할 것 : 패스와 함께이 작업을 수행 할 수있는 방법은 무엇

File parent = new File("c:\\tmp"); 
File child = new File(parent, "child"); // this accesses c:\tmp\child 

?

나는이 작동 가정 :

Path parent = Paths.get("c:\\tmp"); 
Path child = Paths.get(parent.toString(), "child"); 

그러나 parent.toString()를 호출하면 추한 것 같다. 더 좋은 방법이 있습니까?

답변

80

Pathresolve 방법을 사용하십시오.

이 이름에는 두 가지 방법이 있습니다. One은 상대 Paththe otherString으로 취합니다. 부모로 불리는 Path을 사용하고 String 또는 상대 Path을 적절하게 추가합니다.

Path parent = Paths.get("c:\\tmp"); 
Path child = parent.resolve("child"); 
관련 문제