Skip to content

Commit 2a58dcf

Browse files
committed
initial commit
1 parent cc01280 commit 2a58dcf

6 files changed

Lines changed: 256 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.copyfolder;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
6+
import org.apache.commons.io.FileUtils;
7+
8+
public class ApacheCommons {
9+
10+
public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException {
11+
File sourceFolder = new File(sourceDirectoryLocation);
12+
File destinationFolder = new File(destinationDirectoryLocation);
13+
FileUtils.copyDirectory(sourceFolder, destinationFolder);
14+
}
15+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.copyfolder;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
10+
public class CoreOld {
11+
12+
public static void copyDirectoryJavaUnder7(File source, File destination) throws IOException {
13+
if (source.isDirectory()) {
14+
copyDirectory(source, destination);
15+
} else {
16+
copyFile(source, destination);
17+
}
18+
}
19+
20+
private static void copyDirectory(File sourceFolder, File destinationFolder) throws IOException {
21+
if (!destinationFolder.exists()) {
22+
destinationFolder.mkdir();
23+
}
24+
for (String f : sourceFolder.list()) {
25+
copyDirectoryJavaUnder7(new File(sourceFolder, f), new File(destinationFolder, f));
26+
}
27+
}
28+
29+
private static void copyFile(File sourceFile, File destinationFile) throws IOException {
30+
try (InputStream in = new FileInputStream(sourceFile); OutputStream out = new FileOutputStream(destinationFile)) {
31+
byte[] buf = new byte[1024];
32+
int length;
33+
while ((length = in.read(buf)) > 0) {
34+
out.write(buf, 0, length);
35+
}
36+
}
37+
}
38+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.copyfolder;
2+
3+
import java.io.IOException;
4+
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
8+
public class JavaNio {
9+
10+
public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException {
11+
Files.walk(Paths.get(sourceDirectoryLocation))
12+
.forEach(a -> {
13+
Path b = Paths.get(destinationDirectoryLocation, a.toString()
14+
.substring(sourceDirectoryLocation.length()));
15+
try {
16+
Files.copy(a, b);
17+
} catch (IOException e) {
18+
e.printStackTrace();
19+
}
20+
});
21+
}
22+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.copyfolder;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.Comparator;
12+
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
17+
public class ApacheCommonsUnitTest {
18+
19+
private final String sourceFolderLocation = "src/test/resources/sourceFolder";
20+
private final String subFolderName = "/childFolder";
21+
private final String fileName = "/file.txt";
22+
private final String destinationFolderLocation = "src/test/resources/destinationFolder";
23+
24+
@BeforeEach
25+
public void createFolderWithSubfolderAndFile() throws IOException {
26+
Files.createDirectories(Paths.get(sourceFolderLocation));
27+
Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName));
28+
Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName));
29+
}
30+
31+
@Test
32+
public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException {
33+
ApacheCommons.copyDirectory(sourceFolderLocation, destinationFolderLocation);
34+
35+
assertTrue(new File(destinationFolderLocation).exists());
36+
assertTrue(new File(destinationFolderLocation + subFolderName).exists());
37+
assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists());
38+
}
39+
40+
@Test
41+
public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() {
42+
assertThrows(Exception.class, () -> ApacheCommons.copyDirectory("nonExistingFolder", destinationFolderLocation));
43+
}
44+
45+
@AfterEach
46+
public void cleanUp() throws IOException {
47+
Files.walk(Paths.get(sourceFolderLocation))
48+
.sorted(Comparator.reverseOrder())
49+
.map(Path::toFile)
50+
.forEach(File::delete);
51+
if (new File(destinationFolderLocation).exists()) {
52+
Files.walk(Paths.get(destinationFolderLocation))
53+
.sorted(Comparator.reverseOrder())
54+
.map(Path::toFile)
55+
.forEach(File::delete);
56+
}
57+
}
58+
59+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.baeldung.copyfolder;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.Comparator;
12+
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
17+
public class CoreOldUnitTest {
18+
19+
private final String sourceFolderLocation = "src/test/resources/sourceFolder";
20+
private final String subFolderName = "/childFolder";
21+
private final String fileName = "/file.txt";
22+
private final String destinationFolderLocation = "src/test/resources/destinationFolder";
23+
24+
@BeforeEach
25+
public void createFolderWithSubfolderAndFile() throws IOException {
26+
Files.createDirectories(Paths.get(sourceFolderLocation));
27+
Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName));
28+
Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName));
29+
}
30+
31+
@Test
32+
public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException {
33+
File sourceFolder = new File(sourceFolderLocation);
34+
File destinationFolder = new File(destinationFolderLocation);
35+
CoreOld.copyDirectoryJavaUnder7(sourceFolder, destinationFolder);
36+
37+
assertTrue(new File(destinationFolderLocation).exists());
38+
assertTrue(new File(destinationFolderLocation + subFolderName).exists());
39+
assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists());
40+
}
41+
42+
@Test
43+
public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() throws IOException {
44+
File sourceFolder = new File("nonExistingFolder");
45+
File destinationFolder = new File(destinationFolderLocation);
46+
assertThrows(IOException.class, () -> CoreOld.copyDirectoryJavaUnder7(sourceFolder, destinationFolder));
47+
}
48+
49+
@AfterEach
50+
public void cleanUp() throws IOException {
51+
Files.walk(Paths.get(sourceFolderLocation))
52+
.sorted(Comparator.reverseOrder())
53+
.map(Path::toFile)
54+
.forEach(File::delete);
55+
if (new File(destinationFolderLocation).exists()) {
56+
Files.walk(Paths.get(destinationFolderLocation))
57+
.sorted(Comparator.reverseOrder())
58+
.map(Path::toFile)
59+
.forEach(File::delete);
60+
}
61+
}
62+
63+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.copyfolder;
2+
3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.nio.file.Files;
9+
import java.nio.file.Path;
10+
import java.nio.file.Paths;
11+
import java.util.Comparator;
12+
13+
import org.junit.jupiter.api.AfterEach;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
17+
public class JavaNioUnitTest {
18+
19+
private final String sourceFolderLocation = "src/test/resources/sourceFolder";
20+
private final String subFolderName = "/childFolder";
21+
private final String fileName = "/file.txt";
22+
private final String destinationFolderLocation = "src/test/resources/destinationFolder";
23+
24+
@BeforeEach
25+
public void createFolderWithSubfolderAndFile() throws IOException {
26+
Files.createDirectories(Paths.get(sourceFolderLocation));
27+
Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName));
28+
Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName));
29+
}
30+
31+
@Test
32+
public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException {
33+
JavaNio.copyDirectory(sourceFolderLocation, destinationFolderLocation);
34+
35+
assertTrue(new File(destinationFolderLocation).exists());
36+
assertTrue(new File(destinationFolderLocation + subFolderName).exists());
37+
assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists());
38+
}
39+
40+
@Test
41+
public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() {
42+
assertThrows(IOException.class, () -> JavaNio.copyDirectory("nonExistingFolder", destinationFolderLocation));
43+
}
44+
45+
@AfterEach
46+
public void cleanUp() throws IOException {
47+
Files.walk(Paths.get(sourceFolderLocation))
48+
.sorted(Comparator.reverseOrder())
49+
.map(Path::toFile)
50+
.forEach(File::delete);
51+
if (new File(destinationFolderLocation).exists()) {
52+
Files.walk(Paths.get(destinationFolderLocation))
53+
.sorted(Comparator.reverseOrder())
54+
.map(Path::toFile)
55+
.forEach(File::delete);
56+
}
57+
}
58+
59+
}

0 commit comments

Comments
 (0)