File was renamed from src/de/uhilger/fm/CopyMoveVisitor.java |
| | |
| | | import java.nio.file.attribute.BasicFileAttributes; |
| | | |
| | | /** |
| | | * Ein FileVisitor zum Verschieben oder Kopieren ganzer Ordnerstrukturen mit |
| | | * Ein FileVisitor zum Verschieben, Kopieren oder Loeschen ganzer Ordnerstrukturen mit |
| | | * Hilfe der Methode Files.walkFileTree von java.nio. |
| | | * |
| | | * @author Ulrich Hilger |
| | | * @version 1, 14. Mai 2021 |
| | | */ |
| | | public class CopyMoveVisitor extends FileHelper implements FileVisitor { |
| | | public class FileOpsVisitor extends FileHelper implements FileVisitor { |
| | | |
| | | private Path targetDir; |
| | | private int operation; |
| | |
| | | * Ordner ueberschrieben wird, indem am Ziel fuer einen bereits existierenden Ordner ein |
| | | * anderer Name mit laufender Nummer erzeugt wird. |
| | | * |
| | | * Invoked for a directory before entries in the directory are visited. If this method |
| | | * returns CONTINUE, then entries in the directory are visited. If this method returns |
| | | * SKIP_SUBTREE or SKIP_SIBLINGS then entries in the directory (and any descendants) |
| | | * will not be visited. |
| | | * |
| | | * @param dir Zielordner |
| | | * @param attrs die gewuenschten Attribute |
| | | * @return gibt stets FileVisitResult.CONTINUE zurueck |
| | | * @throws IOException wenn etwas schief geht |
| | | */ |
| | | @Override |
| | | public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException { |
| | | public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) |
| | | throws IOException { |
| | | if (operation != Eraser.OP_DELETE) { |
| | | if (dir instanceof Path) { |
| | | Path sourceDir = (Path) dir; |
| | |
| | | return FileVisitResult.CONTINUE; |
| | | } |
| | | |
| | | /** |
| | | * Fuer jede Datei die gewuenschte Dateioperation ausführen |
| | | * |
| | | * Invoked for a file in a directory. |
| | | * |
| | | * @param file die zu bearbeitende Datei a reference to the file |
| | | * @param attrs the directory's basic attributes |
| | | * @return stets FileVisitResult.CONTINUE |
| | | * @throws IOException wenn etwas schief geht |
| | | */ |
| | | @Override |
| | | public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException { |
| | | if(operation != Eraser.OP_DELETE) { |
| | |
| | | destFile = getNewFileName(destFile); |
| | | } |
| | | if (operation == Mover.OP_MOVE) { |
| | | //logger.fine("move source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); |
| | | Files.move(source, destFile.toPath()); |
| | | } else if (operation == Mover.OP_COPY) { |
| | | //logger.fine("copy source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); |
| | | Files.copy(source, destFile.toPath()); |
| | | } |
| | | } |
| | |
| | | return FileVisitResult.CONTINUE; |
| | | } |
| | | |
| | | /** |
| | | * Bei diesem Visitor bleibt diese Methode ungenutzt, hier muessten noch Faelle |
| | | * behandelt werden, die zu einem Abbruch fuehren und ggf. ein Rollback realisiert werden. |
| | | * |
| | | * Invoked for a file that could not be visited. This method is invoked if the file's attributes |
| | | * could not be read, the file is a directory that could not be opened, and other reasons. |
| | | * |
| | | * @param file die Datei, bei der es zum Abbruch kam |
| | | * @param exc the I/O exception that prevented the file from being visited |
| | | * @return stets FileVisitResult.CONTINUE |
| | | * @throws IOException wenn etwas schief laeuft |
| | | */ |
| | | @Override |
| | | public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException { |
| | | return FileVisitResult.CONTINUE; |
| | | } |
| | | |
| | | /** |
| | | * Fuer jede Datei Schritte ausfuehren, wie sie sich nach einer Dateioperation ergeben. |
| | | * Hier wird beim Verschieben von Dateien das Quellverzeichnis geloescht, nachdem es zum |
| | | * Ziel uebertragen wurde. |
| | | * |
| | | * Invoked for a directory after entries in the directory, and all of their descendants, |
| | | * have been visited. This method is also invoked when iteration of the directory completes |
| | | * prematurely (by a visitFile method returning SKIP_SIBLINGS, or an I/O error when |
| | | * iterating over the directory). |
| | | * |
| | | * @param dir der fertig durchlaufene Quellordner |
| | | * @param exc null if the iteration of the directory completes without an error; otherwise |
| | | * the I/O exception that caused the iteration of the directory to complete prematurely |
| | | * @return |
| | | * @throws IOException |
| | | */ |
| | | @Override |
| | | public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException { |
| | | if (operation != Eraser.OP_DELETE) { |