Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void copy(File sourceLocation, File targetLocation) throws IOException {
- if (sourceLocation.isDirectory()) {
- copyDirectory(sourceLocation, targetLocation);
- } else {
- copyFile(sourceLocation, targetLocation);
- }
- }
- private void copyDirectory(File source, File target) throws IOException {
- if (!target.exists()) {
- target.mkdir();
- }
- for (String f : source.list()) {
- copy(new File(source, f), new File(target, f));
- }
- }
- private void copyFile(File source, File target) throws IOException {
- try (
- InputStream in = new FileInputStream(source);
- OutputStream out = new FileOutputStream(target)
- ) {
- byte[] buf = new byte[1024];
- int length;
- while ((length = in.read(buf)) > 0) {
- out.write(buf, 0, length);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement