문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 | ||
java:7:files [2011/09/14 16:05] kwon37xi 새로 만듦 |
java:7:files [2022/04/05 23:59] kwon37xi |
||
---|---|---|---|
줄 1: | 줄 1: | ||
====== Java 7 Files ====== | ====== Java 7 Files ====== | ||
NIO. 2 에 파일 관련 각종 API가 추가되었다. 이전보다 더 쉽게 파일 관련 작업을 수행할 수 있다. | NIO. 2 에 파일 관련 각종 API가 추가되었다. 이전보다 더 쉽게 파일 관련 작업을 수행할 수 있다. | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | * [[http:// | ||
+ | ===== Path ===== | ||
+ | 파일의 경로는 [[http:// | ||
+ | |||
+ | Path 는 인터페이스이며 [[http:// | ||
+ | |||
+ | <code java> | ||
+ | import java.nio.file.FileSystems; | ||
+ | import java.nio.file.Files; | ||
+ | import java.nio.file.Path; | ||
+ | import java.nio.file.Paths; | ||
+ | |||
+ | Path path = Paths.get(" | ||
+ | //혹은 FileSystem 객체로 부터... | ||
+ | Path pathFromFS = FileSystems.getDefault().getPath(" | ||
+ | |||
+ | System.out.println(" | ||
+ | </ | ||
+ | |||
+ | ===== Files ===== | ||
+ | [[http:// | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | [[http:// | ||
+ | |||
+ | ===== Directory 삭제 ===== | ||
+ | * [[https:// | ||
+ | <code java> | ||
+ | public void whenDeletedWithNIO2WalkFileTree_thenIsGone() | ||
+ | throws IOException { | ||
+ | |||
+ | Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME); | ||
+ | |||
+ | Files.walkFileTree(pathToBeDeleted, | ||
+ | new SimpleFileVisitor< | ||
+ | @Override | ||
+ | public FileVisitResult postVisitDirectory( | ||
+ | Path dir, IOException exc) throws IOException { | ||
+ | Files.delete(dir); | ||
+ | return FileVisitResult.CONTINUE; | ||
+ | } | ||
+ | | ||
+ | @Override | ||
+ | public FileVisitResult visitFile( | ||
+ | Path file, BasicFileAttributes attrs) | ||
+ | throws IOException { | ||
+ | Files.delete(file); | ||
+ | return FileVisitResult.CONTINUE; | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | assertFalse(" | ||
+ | Files.exists(pathToBeDeleted)); | ||
+ | } | ||
+ | </ | ||
+ | |||