initial commit

This commit is contained in:
Maja Joksovic 2020-07-28 01:33:43 +02:00
parent cc012802de
commit 2a58dcf29d
6 changed files with 256 additions and 0 deletions

View File

@ -0,0 +1,15 @@
package com.baeldung.copyfolder;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class ApacheCommons {
public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException {
File sourceFolder = new File(sourceDirectoryLocation);
File destinationFolder = new File(destinationDirectoryLocation);
FileUtils.copyDirectory(sourceFolder, destinationFolder);
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.copyfolder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CoreOld {
public static void copyDirectoryJavaUnder7(File source, File destination) throws IOException {
if (source.isDirectory()) {
copyDirectory(source, destination);
} else {
copyFile(source, destination);
}
}
private static void copyDirectory(File sourceFolder, File destinationFolder) throws IOException {
if (!destinationFolder.exists()) {
destinationFolder.mkdir();
}
for (String f : sourceFolder.list()) {
copyDirectoryJavaUnder7(new File(sourceFolder, f), new File(destinationFolder, f));
}
}
private static void copyFile(File sourceFile, File destinationFile) throws IOException {
try (InputStream in = new FileInputStream(sourceFile); OutputStream out = new FileOutputStream(destinationFile)) {
byte[] buf = new byte[1024];
int length;
while ((length = in.read(buf)) > 0) {
out.write(buf, 0, length);
}
}
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.copyfolder;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JavaNio {
public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException {
Files.walk(Paths.get(sourceDirectoryLocation))
.forEach(a -> {
Path b = Paths.get(destinationDirectoryLocation, a.toString()
.substring(sourceDirectoryLocation.length()));
try {
Files.copy(a, b);
} catch (IOException e) {
e.printStackTrace();
}
});
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.copyfolder;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class ApacheCommonsUnitTest {
private final String sourceFolderLocation = "src/test/resources/sourceFolder";
private final String subFolderName = "/childFolder";
private final String fileName = "/file.txt";
private final String destinationFolderLocation = "src/test/resources/destinationFolder";
@BeforeEach
public void createFolderWithSubfolderAndFile() throws IOException {
Files.createDirectories(Paths.get(sourceFolderLocation));
Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName));
Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName));
}
@Test
public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException {
ApacheCommons.copyDirectory(sourceFolderLocation, destinationFolderLocation);
assertTrue(new File(destinationFolderLocation).exists());
assertTrue(new File(destinationFolderLocation + subFolderName).exists());
assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists());
}
@Test
public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() {
assertThrows(Exception.class, () -> ApacheCommons.copyDirectory("nonExistingFolder", destinationFolderLocation));
}
@AfterEach
public void cleanUp() throws IOException {
Files.walk(Paths.get(sourceFolderLocation))
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
if (new File(destinationFolderLocation).exists()) {
Files.walk(Paths.get(destinationFolderLocation))
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
}

View File

@ -0,0 +1,63 @@
package com.baeldung.copyfolder;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class CoreOldUnitTest {
private final String sourceFolderLocation = "src/test/resources/sourceFolder";
private final String subFolderName = "/childFolder";
private final String fileName = "/file.txt";
private final String destinationFolderLocation = "src/test/resources/destinationFolder";
@BeforeEach
public void createFolderWithSubfolderAndFile() throws IOException {
Files.createDirectories(Paths.get(sourceFolderLocation));
Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName));
Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName));
}
@Test
public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException {
File sourceFolder = new File(sourceFolderLocation);
File destinationFolder = new File(destinationFolderLocation);
CoreOld.copyDirectoryJavaUnder7(sourceFolder, destinationFolder);
assertTrue(new File(destinationFolderLocation).exists());
assertTrue(new File(destinationFolderLocation + subFolderName).exists());
assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists());
}
@Test
public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() throws IOException {
File sourceFolder = new File("nonExistingFolder");
File destinationFolder = new File(destinationFolderLocation);
assertThrows(IOException.class, () -> CoreOld.copyDirectoryJavaUnder7(sourceFolder, destinationFolder));
}
@AfterEach
public void cleanUp() throws IOException {
Files.walk(Paths.get(sourceFolderLocation))
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
if (new File(destinationFolderLocation).exists()) {
Files.walk(Paths.get(destinationFolderLocation))
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
}

View File

@ -0,0 +1,59 @@
package com.baeldung.copyfolder;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Comparator;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class JavaNioUnitTest {
private final String sourceFolderLocation = "src/test/resources/sourceFolder";
private final String subFolderName = "/childFolder";
private final String fileName = "/file.txt";
private final String destinationFolderLocation = "src/test/resources/destinationFolder";
@BeforeEach
public void createFolderWithSubfolderAndFile() throws IOException {
Files.createDirectories(Paths.get(sourceFolderLocation));
Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName));
Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName));
}
@Test
public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException {
JavaNio.copyDirectory(sourceFolderLocation, destinationFolderLocation);
assertTrue(new File(destinationFolderLocation).exists());
assertTrue(new File(destinationFolderLocation + subFolderName).exists());
assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists());
}
@Test
public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() {
assertThrows(IOException.class, () -> JavaNio.copyDirectory("nonExistingFolder", destinationFolderLocation));
}
@AfterEach
public void cleanUp() throws IOException {
Files.walk(Paths.get(sourceFolderLocation))
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
if (new File(destinationFolderLocation).exists()) {
Files.walk(Paths.get(destinationFolderLocation))
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
}