Skip to content

Commit f7e92e3

Browse files
committed
BAEL-1308
1 parent b7b5543 commit f7e92e3

2 files changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.copyfiles;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
5+
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileOutputStream;
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.OutputStream;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.nio.file.StandardCopyOption;
14+
15+
import org.apache.commons.io.FileUtils;
16+
17+
public class FileCopier {
18+
public static File copyWithIO(File original, File copied) throws IOException {
19+
try (InputStream in = new BufferedInputStream(new FileInputStream(original));
20+
OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) {
21+
byte[] buffer = new byte[1024];
22+
int lengthRead;
23+
while ((lengthRead = in.read(buffer)) > 0) {
24+
out.write(buffer, 0, lengthRead);
25+
out.flush();
26+
}
27+
}
28+
return copied;
29+
}
30+
31+
public static Path copyWithNio(Path original, Path copied) throws IOException {
32+
Files.copy(original, copied, StandardCopyOption.REPLACE_EXISTING);
33+
return copied;
34+
}
35+
36+
public static File copyWithCommonsIO(File original, File copied) throws IOException {
37+
FileUtils.copyFile(original, copied);
38+
return copied;
39+
}
40+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.copyfiles;
2+
3+
import static org.junit.Assert.*;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.Paths;
10+
11+
import org.junit.Test;
12+
13+
public class FileCopierTest {
14+
15+
@Test
16+
public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
17+
File copied = new File("src/test/resources/copiedWithIo.txt");
18+
File original = new File("src/test/resources/original.txt");
19+
copied = FileCopier.copyWithIO(original, copied);
20+
assertTrue(copied.exists());
21+
assertTrue(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
22+
}
23+
24+
@Test
25+
public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
26+
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
27+
File original = new File("src/test/resources/original.txt");
28+
copied = FileCopier.copyWithCommonsIO(original, copied);
29+
assertTrue(copied.exists());
30+
assertTrue(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
31+
}
32+
33+
@Test
34+
public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException {
35+
Path copied = Paths.get("src/test/resources/copiedWithNio.txt");
36+
Path original = Paths.get("src/test/resources/original.txt");
37+
copied = FileCopier.copyWithNio(original, copied);
38+
assertTrue(Files.exists(copied));
39+
assertTrue(Files.readAllLines(original).equals(Files.readAllLines(copied)));
40+
}
41+
}

0 commit comments

Comments
 (0)