1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
| import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.OpenOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class FileTree {
public static void main(String[] args) {
try {
// écrit l'arborescence du dossier D:/FILES/DOC à la suite dans le fichier "C:/TEMP/tree.txt"
new FileTree().writeTree("D:/FILES/PHOTOS","C:/TEMP/tree.txt",StandardOpenOption.APPEND);
// affiche le contenu du fichier créé en console
Files.lines(Paths.get("C:/TEMP/tree.txt")).forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
public void writeTree(String dirPath, String targetPath, OpenOption...options) throws IOException {
writeTree(dirPath, targetPath, 4, options);
}
public void writeTree(String dirPath, String targetPath, int indentAmount, OpenOption...options) throws IOException {
Path path = Paths.get(dirPath);
if ( !Files.exists(path) ) throw new FileNotFoundException("Directory not found: "+ dirPath);
else if ( !Files.isDirectory(path) ) throw new IOException("Not a directory: "+dirPath);
Path target = Paths.get(targetPath);
if ( !Files.exists(target) ) {
Set<OpenOption> o = new HashSet<>(Arrays.asList(options));
Path targetDir = target.getParent();
if ( o.contains(StandardOpenOption.CREATE) ) {
if ( Files.isDirectory(targetDir) ) {
throw new IOException(targetDir + " is exist as a file");
}
Files.createDirectory(targetDir);
}
else {
if ( o.contains(StandardOpenOption.APPEND) ) {
o.add(StandardOpenOption.CREATE);
options = o.toArray(new OpenOption[o.size()]);
}
}
}
try(PrintWriter writer = new PrintWriter(Files.newBufferedWriter(target,options))) {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
int indent = 0;
int filecount = 0;
@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
if ( dir.equals(path)) {
writer.print("Tree of ");
writer.println(dir);
}
else {
indent(writer);
writer.print("Dir: ");
writer.println(dir);
}
indent+=indentAmount;
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
indent-=indentAmount;
if ( dir.equals(path)) {
writer.println("File count: "+filecount);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if ( !Files.isHidden(file) ) {
indent(writer);
writer.println(file);
filecount++;
}
return FileVisitResult.CONTINUE;
}
private void indent(PrintWriter writer) throws IOException {
for(int i=0; i<indent; i++) {
writer.write(' ');
}
}
});
}
}
} |
Partager