Abstract
JDK 7에 포함된 java.nio.file 패키지의 FileVisitor를 이용하여 Window7 에서 긴 파일명을 삭제하지 못해 디렉터리를 삭제할 수 없을 경우에 아래와 같이 사용하면 된다.Visitor 패턴의 대표적인 유즈케이스인 디렉터리를 리커시브하게 둘러치며 파일 찝적대는 프로그래밍하던 수고를 JavaSE 1.7 안에 포함시켰다.
DerectoryDelete
----------------------------------------------------------
package toughguy.nicholas.utils.file;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
public class DirectoryDelete extends SimpleFileVisitor
private final File basedir;
public DirectoryDelete(File basedir) {
super();
this.basedir = basedir;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
if(attrs.isRegularFile()) {
System.out.println("delete file: " + file.getFileName());
Files.delete(file);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
System.out.println("delete directory: " + dir);
Files.delete(dir);
return FileVisitResult.CONTINUE;
}
public void delete() throws IOException {
Path path = Paths.get(this.basedir.toURI());
Files.walkFileTree(path, this);
}
}
------------------------------------------------------------------
테스트 코드
----------------------------------------------------------
package toughguy.nicholas.utils.file;
import java.io.File;
import org.junit.Test;
public class DirectoryDeleteTest {
@Test
public void testDirectoryDelete() throws Exception {
File dir = new File("C:/WebCenterSites");
DirectoryDelete directoryDelete = new DirectoryDelete(dir);
directoryDelete.delete();
}
}
----------------------------------------------------------
Resources:
microtouch Solo Titanium Tipped Steel Razor - TITanium-Art
답글삭제Microtouch titanium dental implants and periodontics Solo Titanium Tipped 2016 ford focus titanium Steel Razor Features titanium tent stove Microtouch, Nickel plating, non-refurbished, and a snap titanium pan closure black titanium rings feature.