사용자 도구

사이트 도구


java:7:files

차이

문서의 선택한 두 판 사이의 차이를 보여줍니다.

차이 보기로 링크

다음 판
이전 판
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://download.oracle.com/javase/tutorial/essential/io/fileio.html|File I/O (Featuring NIO.2) (The Java™ Tutorials > Essential Classes > Basic I/O)]] 에서 자세히 볼 수 있으며 아래는 간단한 정리이다..
 +
 +  * [[http://www.javabeat.net/2012/06/creating-writing-reading-java-files-api-java-7/|Creating, Writing, Reading files using Java Files API of Java 7]]
 +===== Path =====
 +파일의 경로는 [[http://download.oracle.com/javase/7/docs/api/java/nio/file/Path.html|java.nio.file.Path]] 인터페이스로 나타낸다. 기존의 [[http://download.oracle.com/javase/7/docs/api/java/io/File.html|java.io.File]]을 Path로 대체한다.
 +
 +Path 는 인터페이스이며 [[http://download.oracle.com/javase/7/docs/api/java/nio/file/Paths.html|Paths]] 혹은 [[http://download.oracle.com/javase/7/docs/api/java/nio/file/FileSystem.html|FileSystem]] 객체로부터 Path 객체를 생성할 수 있다.
 +
 +<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("C:\\Temp\\test.txt");
 +//혹은 FileSystem 객체로 부터...
 +Path pathFromFS = FileSystems.getDefault().getPath("C:\\Temp\\test.txt");
 +
 +System.out.println("Path exists == " + Files.exists(path));
 +</code>
 +
 +===== Files =====
 +[[http://download.oracle.com/javase/7/docs/api/java/nio/file/Files.html|Files]] 클래스는 파일의 복사/이동/삭제/스트림생성/각종 파일 속성 설정 등에 관련된 매우 쉬운 static 메소드를 수십여개 제공해주고 있다.
 +
 +[[http://www.java7developer.com/blog/?p=334|Manipulating Files in Java 7]]와 [[http://download.oracle.com/javase/tutorial/essential/io/fileio.html|File I/O (Featuring NIO.2) (The Java™ Tutorials > Essential Classes > Basic I/O)]]를 참조한다.
 +
 +[[http://www.javacodegeeks.com/2012/02/java-7-copy-and-move-files-and.html|Java 7: Copy and Move Files and Directories - Java Code Geeks]]
 +
 +[[http://docs.oracle.com/javase/tutorial/essential/io/notification.html|Watching a Directory for Changes (The Java™ Tutorials > Essential Classes > Basic I/O)]]
 +
 +===== Directory 삭제 =====
 +  * [[https://www.baeldung.com/java-delete-directory|Delete a Directory Recursively in Java | Baeldung]]
 +<code java>
 +public void whenDeletedWithNIO2WalkFileTree_thenIsGone() 
 +  throws IOException {
 + 
 +    Path pathToBeDeleted = TEMP_DIRECTORY.resolve(DIRECTORY_NAME);
 +
 +    Files.walkFileTree(pathToBeDeleted, 
 +      new SimpleFileVisitor<Path>() {
 +        @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("Directory still exists", 
 +      Files.exists(pathToBeDeleted));
 +}
 +</code>
 +
  
java/7/files.1315983952.txt.gz · 마지막으로 수정됨: 2011/09/14 16:05 저자 kwon37xi