Skip to content

Commit 49541d8

Browse files
committed
added copyDir method
1 parent 282794c commit 49541d8

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

mode/src/processing/mode/android/AndroidUtil.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,12 @@
3333
import java.util.zip.ZipFile;
3434
import java.nio.file.Files;
3535
import java.nio.file.StandardCopyOption;
36-
36+
import java.nio.file.Paths;
37+
import java.nio.file.Path;
38+
import java.nio.file.SimpleFileVisitor;
39+
import java.nio.file.FileVisitResult;
40+
import java.nio.file.attribute.BasicFileAttributes;
41+
3742
import javax.swing.JEditorPane;
3843
import javax.swing.JLabel;
3944
import javax.swing.JOptionPane;
@@ -306,4 +311,37 @@ static public void moveDir(File from, File to) {
306311
ex.printStackTrace();
307312
}
308313
}
314+
315+
static public void copyDir(File from, File to) {
316+
final Path source = Paths.get(from.toURI());
317+
final Path target = Paths.get(to.toURI());
318+
319+
SimpleFileVisitor copyVisitor = new SimpleFileVisitor<Path>() {
320+
@Override
321+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
322+
Path resolve = target.resolve(source.relativize(dir));
323+
if (Files.notExists(resolve)) Files.createDirectories(resolve);
324+
return FileVisitResult.CONTINUE;
325+
}
326+
327+
@Override
328+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
329+
Path resolve = target.resolve(source.relativize(file));
330+
Files.copy(file, resolve, StandardCopyOption.REPLACE_EXISTING);
331+
return FileVisitResult.CONTINUE;
332+
}
333+
334+
@Override
335+
public FileVisitResult visitFileFailed(Path file, IOException exc) {
336+
System.err.format("Unable to copy: %s: %s%n", file, exc);
337+
return FileVisitResult.CONTINUE;
338+
}
339+
};
340+
341+
try {
342+
Files.walkFileTree(source, copyVisitor);
343+
} catch (IOException e) {
344+
e.printStackTrace();
345+
}
346+
}
309347
}

0 commit comments

Comments
 (0)