From cf7c96b469b22c7bc446d3992e51487019041e33 Mon Sep 17 00:00:00 2001 From: Ahmad Alsanie Date: Mon, 20 Nov 2017 18:05:47 +0200 Subject: [PATCH] BAEL-1308 - copy a file with java (Editor Notes) --- .../com/baeldung/copyfiles/FileCopier.java | 40 --------- .../baeldung/copyfiles/FileCopierTest.java | 88 ++++++++++++------- 2 files changed, 55 insertions(+), 73 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/copyfiles/FileCopier.java diff --git a/core-java/src/main/java/com/baeldung/copyfiles/FileCopier.java b/core-java/src/main/java/com/baeldung/copyfiles/FileCopier.java deleted file mode 100644 index 0841cd67a3..0000000000 --- a/core-java/src/main/java/com/baeldung/copyfiles/FileCopier.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.copyfiles; - -import java.io.BufferedInputStream; -import java.io.BufferedOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; - -import org.apache.commons.io.FileUtils; - -public class FileCopier { - public static File copyWithIO(File original, File copied) throws IOException { - try (InputStream in = new BufferedInputStream(new FileInputStream(original)); - OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) { - byte[] buffer = new byte[1024]; - int lengthRead; - while ((lengthRead = in.read(buffer)) > 0) { - out.write(buffer, 0, lengthRead); - out.flush(); - } - } - return copied; - } - - public static Path copyWithNio(Path original, Path copied) throws IOException { - Files.copy(original, copied, StandardCopyOption.REPLACE_EXISTING); - return copied; - } - - public static File copyWithCommonsIO(File original, File copied) throws IOException { - FileUtils.copyFile(original, copied); - return copied; - } -} diff --git a/core-java/src/test/java/com/baeldung/copyfiles/FileCopierTest.java b/core-java/src/test/java/com/baeldung/copyfiles/FileCopierTest.java index 9c95dcec47..973436a26a 100644 --- a/core-java/src/test/java/com/baeldung/copyfiles/FileCopierTest.java +++ b/core-java/src/test/java/com/baeldung/copyfiles/FileCopierTest.java @@ -1,48 +1,70 @@ package com.baeldung.copyfiles; -import static org.junit.Assert.*; - +import java.io.BufferedInputStream; +import java.io.BufferedOutputStream; import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Test; +import static org.assertj.core.api.Assertions.*; public class FileCopierTest { - File original =new File("src/test/resources/original.txt"); - @Before - public void init() throws IOException{ - if(!original.exists()) - Files.createFile(original.toPath()); - } - @Test - public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { - File copied = new File("src/test/resources/copiedWithIo.txt"); - copied = FileCopier.copyWithIO(original, copied); - assertTrue(copied.exists()); - assertTrue(Files.readAllLines(original.toPath()) - .equals(Files.readAllLines(copied.toPath()))); - } + File original = new File("src/test/resources/original.txt"); - @Test - public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { - File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); - copied = FileCopier.copyWithCommonsIO(original, copied); - assertTrue(copied.exists()); - assertTrue(Files.readAllLines(original.toPath()) - .equals(Files.readAllLines(copied.toPath()))); - } + @Before + public void init() throws IOException { + if (!original.exists()) + Files.createFile(original.toPath()); + } - @Test - public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException { - Path copied = Paths.get("src/test/resources/copiedWithNio.txt"); - Path originalPath = original.toPath(); - copied = FileCopier.copyWithNio(originalPath, copied); - assertTrue(Files.exists(copied)); - assertTrue(Files.readAllLines(originalPath) - .equals(Files.readAllLines(copied))); - } + @Test + public void givenIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { + File copied = new File("src/test/resources/copiedWithIo.txt"); + try (InputStream in = new BufferedInputStream(new FileInputStream(original)); + OutputStream out = new BufferedOutputStream(new FileOutputStream(copied))) { + byte[] buffer = new byte[1024]; + int lengthRead; + while ((lengthRead = in.read(buffer)) > 0) { + out.write(buffer, 0, lengthRead); + out.flush(); + } + } + assertThat(copied).exists(); + assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); + } + + @Test + public void givenCommonsIoAPI_whenCopied_thenCopyExistsWithSameContents() throws IOException { + File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); + FileUtils.copyFile(original, copied); + assertThat(copied).exists(); + assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); + } + + @Test + public void givenNIO2_whenCopied_thenCopyExistsWithSameContents() throws IOException { + Path copied = Paths.get("src/test/resources/copiedWithNio.txt"); + Path originalPath = original.toPath(); + Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING); + assertThat(copied).exists(); + assertThat(Files.readAllLines(originalPath).equals(Files.readAllLines(copied))); + } + + @Test + public void givenGuava_whenCopied_thenCopyExistsWithSameContents() throws IOException { + File copied = new File("src/test/resources/copiedWithApacheCommons.txt"); + com.google.common.io.Files.copy(original, copied); + assertThat(copied).exists(); + assertThat(Files.readAllLines(original.toPath()).equals(Files.readAllLines(copied.toPath()))); + } }