Skip to content

Commit cf7c96b

Browse files
committed
BAEL-1308 - copy a file with java (Editor Notes)
1 parent 8b2d647 commit cf7c96b

2 files changed

Lines changed: 57 additions & 75 deletions

File tree

‎core-java/src/main/java/com/baeldung/copyfiles/FileCopier.java‎

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,70 @@
11
package com.baeldung.copyfiles;
22

3-
import static org.junit.Assert.*;
4-
3+
import java.io.BufferedInputStream;
4+
import java.io.BufferedOutputStream;
55
import java.io.File;
6+
import java.io.FileInputStream;
7+
import java.io.FileOutputStream;
68
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.OutputStream;
711
import java.nio.file.Files;
812
import java.nio.file.Path;
913
import java.nio.file.Paths;
14+
import java.nio.file.StandardCopyOption;
1015

16+
import org.apache.commons.io.FileUtils;
1117
import org.junit.Before;
1218
import org.junit.Test;
19+
import static org.assertj.core.api.Assertions.*;
1320

1421
public class FileCopierTest {
15-
File original =new File("src/test/resources/original.txt");
16-
@Before
17-
public void init() throws IOException{
18-
if(!original.exists())
19-
Files.createFile(original.toPath());
20-
}
21-
@Test
22-
public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
23-
File copied = new File("src/test/resources/copiedWithIo.txt");
24-
copied = FileCopier.copyWithIO(original, copied);
25-
assertTrue(copied.exists());
26-
assertTrue(Files.readAllLines(original.toPath())
27-
.equals(Files.readAllLines(copied.toPath())));
28-
}
29-
30-
@Test
31-
public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
32-
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
33-
copied = FileCopier.copyWithCommonsIO(original, copied);
34-
assertTrue(copied.exists());
35-
assertTrue(Files.readAllLines(original.toPath())
36-
.equals(Files.readAllLines(copied.toPath())));
37-
}
38-
39-
@Test
40-
public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException {
41-
Path copied = Paths.get("src/test/resources/copiedWithNio.txt");
42-
Path originalPath = original.toPath();
43-
copied = FileCopier.copyWithNio(originalPath, copied);
44-
assertTrue(Files.exists(copied));
45-
assertTrue(Files.readAllLines(originalPath)
46-
.equals(Files.readAllLines(copied)));
47-
}
22+
File original = new File("src/test/resources/original.txt");
23+
24+
@Before
25+
public void init() throws IOException {
26+
if (!original.exists())
27+
Files.createFile(original.toPath());
28+
}
29+
30+
@Test
31+
public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
32+
File copied = new File("src/test/resources/copiedWithIo.txt");
33+
try (InputStream in = new BufferedInputStream(new FileInputStream(original));
34+
OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) {
35+
byte[] buffer = new byte[1024];
36+
int lengthRead;
37+
while ((lengthRead = in.read(buffer)) > 0) {
38+
out.write(buffer, 0, lengthRead);
39+
out.flush();
40+
}
41+
}
42+
assertThat(copied).exists();
43+
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
44+
}
45+
46+
@Test
47+
public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException {
48+
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
49+
FileUtils.copyFile(original, copied);
50+
assertThat(copied).exists();
51+
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
52+
}
53+
54+
@Test
55+
public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException {
56+
Path copied = Paths.get("src/test/resources/copiedWithNio.txt");
57+
Path originalPath = original.toPath();
58+
Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING);
59+
assertThat(copied).exists();
60+
assertThat(Files.readAllLines(originalPath).equals(Files.readAllLines(copied)));
61+
}
62+
63+
@Test
64+
public void givenGuava_whenCopied_thenCopyExistsWithSameContents() throws IOException {
65+
File copied = new File("src/test/resources/copiedWithApacheCommons.txt");
66+
com.google.common.io.Files.copy(original, copied);
67+
assertThat(copied).exists();
68+
assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath())));
69+
}
4870
}

0 commit comments

Comments
 (0)