-
[NIO.2]The Path ClassJAVA/NIO2 2013. 11. 13. 23:49728x90
새롭게 JAVA7 에서 추가된 java.nio.file package는 file I/O관련 되어 직관이고 많은 기능의 Class들을 제공 한다.
물론 기존 java.io package 역시 계속 제공 된다.(@Deprecated 되지 않음)
일명 NIO.2 로 명명된 새로운 package의 사용법에 대하여 알아보는 첫번째 시간으로 오늘은 Path Class에 대해 알아 보자
1. Creating a Path
Path p1 = Paths.get("/tmp/foo"); Path p2 = Paths.get(args[0]); Path p3 = Paths.get(URI.create("file:///Users/joe/FileTest.java")); Path p4 = FileSystems.getDefault().getPath("/users/sally"); Path p5 = Paths.get(System.getProperty("user.home"),"logs", "foo.log");
2. Retrieving Information about a Path
2.1 absolute path
// None of these methods requires that the file corresponding // to the Path exists. // Microsoft Windows syntax Path path = Paths.get("C:\\home\\joe\\foo"); // Solaris syntax Path path = Paths.get("/home/joe/foo"); System.out.format("toString: %s%n", path.toString()); System.out.format("getFileName: %s%n", path.getFileName()); System.out.format("getName(0): %s%n", path.getName(0)); System.out.format("getNameCount: %d%n", path.getNameCount()); System.out.format("subpath(0,2): %s%n", path.subpath(0,2)); System.out.format("getParent: %s%n", path.getParent()); System.out.format("getRoot: %s%n", path.getRoot());
결과 :
toString: C:\home\joe\foo
getFileName: foo
getName(0): home
getNameCount: 3
subpath(0,2): home\joe
getParent: C:\home\joe
getRoot: C:\
2.2 relative path
// Solaris syntax Path path = Paths.get("sally/bar"); or // Microsoft Windows syntax Path path = Paths.get("sally\\bar");
결과 :
toString: sally\bar
getFileName: bar
getName(0): sally
getNameCount: 2
subpath(0,2): sally\bar
getParent: sally
getRoot: null
2. Removing Redundancies From a Path
//다음 PATH는 같은 위치를 가리킨다. Path path1 = Paths.get("/home/./joe/foo"); Path path2 = Paths.get("/home/sally/../joe/foo");
3. Converting a Path
Path p1 = Paths.get("/home/logfile"); // Result is file:///home/logfile System.out.format("%s%n", p1.toUri()); //절대 경로 Path fullPath = p1.toAbsolutePath(); //파일이 존재 하는지 확인 하는 명령어. try { Path fp = path.toRealPath(); } catch (NoSuchFileException x) { System.err.format("%s: no such" + " file or directory%n", path); // Logic for case when file doesn't exist. } catch (IOException x) { System.err.format("%s%n", x); // Logic for other sort of file error. }
4. Joining Two Paths
resolve method는 기존 Path에 추가한 결과 값을 돌려 준다.
// Solaris Path p1 = Paths.get("/home/joe/foo"); // Result is /home/joe/foo/bar System.out.format("%s%n", p1.resolve("bar")); or // Microsoft Windows Path p1 = Paths.get("C:\\home\\joe\\foo"); // Result is C:\home\joe\foo\bar System.out.format("%s%n", p1.resolve("bar")); // Result is /home/joe Paths.get("foo").resolve("/home/joe");
5. Creating a Path Between Two Paths
relativeze 는 두개의 주소를 비교하여 차이를 Path로 돌려준다.
Path p1 = Paths.get("joe"); Path p2 = Paths.get("sally"); // Result is ../sally Path p1_to_p2 = p1.relativize(p2); // Result is ../joe Path p2_to_p1 = p2.relativize(p1); Path p1 = Paths.get("home"); Path p3 = Paths.get("home/sally/bar"); // Result is sally/bar Path p1_to_p3 = p1.relativize(p3); // Result is ../.. Path p3_to_p1 = p3.relativize(p1);
6. Comparing Two Paths
Path path = ...; Path otherPath = ...; Path beginning = Paths.get("/home"); Path ending = Paths.get("foo"); if (path.equals(otherPath)) { // equality logic here } else if (path.startsWith(beginning)) { // path begins with "/home" } else if (path.endsWith(ending)) { // path ends with "foo" } Path path = ...; for (Path name: path) { System.out.println(name); }
참고 URL : http://docs.oracle.com/javase/tutorial/essential/io/fileio.html
728x90'JAVA > NIO2' 카테고리의 다른 글
[NIO.2]Copying a File or Directory (0) 2013.11.25 [NIO.2]Deleting a File or Directory (0) 2013.11.25 [NIO.2]Checking a File or Directory (0) 2013.11.25 [NIO.2]Files Class (0) 2013.11.20 [JAVA.7]NIO2 노트 시작 합니다. (0) 2013.11.12