문서의 이전 판입니다!
NIO. 2 에 파일 관련 각종 API가 추가되었다. 이전보다 더 쉽게 파일 관련 작업을 수행할 수 있다.
File I/O (Featuring NIO.2) (The Java™ Tutorials > Essential Classes > Basic I/O) 에서 자세히 볼 수 있으며 아래는 간단한 정리이다..
파일의 경로는 java.nio.file.Path 인터페이스로 나타낸다. 기존의 java.io.File을 Path로 대체한다.
Path 는 인터페이스이며 Paths 혹은 FileSystem 객체로부터 Path 객체를 생성할 수 있다.
import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; Path path = Paths.get("C:\\Temp\\test.txt"); //혹은 FileSystem 객체로 부터... Path pathFromFS = FileSystems.getDefault().getPath("C:\\Temp\\test.txt"); System.out.println("Path exists == " + Files.exists(path));
Files 클래스는 파일의 복사/이동/삭제/스트림생성/각종 파일 속성 설정 등에 관련된 매우 쉬운 static 메소드를 수십여개 제공해주고 있다.
Manipulating Files in Java 7와 File I/O (Featuring NIO.2) (The Java™ Tutorials > Essential Classes > Basic I/O)를 참조한다.
Java 7: Copy and Move Files and Directories - Java Code Geeks
Watching a Directory for Changes (The Java™ Tutorials > Essential Classes > Basic I/O)