diff --git a/core-java/src/main/java/com/baeldung/stream/FileCopy.java b/core-java/src/main/java/com/baeldung/stream/FileCopy.java new file mode 100644 index 0000000000..ccc2066b36 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/stream/FileCopy.java @@ -0,0 +1,48 @@ +package com.baeldung.stream; + +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.channels.FileChannel; +import java.nio.file.Files; + +import org.apache.commons.io.FileUtils; + +public class FileCopy { + + public static void copyFileUsingStream(File source, File dest) throws IOException { + InputStream is = null; + OutputStream os = null; + is = new FileInputStream(source); + os = new FileOutputStream(dest); + byte[] buffer = new byte[1024]; + int length; + while ((length = is.read(buffer)) > 0) { + os.write(buffer, 0, length); + } + is.close(); + os.close(); + } + + public static void copyFileUsingChannel(File source, File dest) throws IOException { + FileChannel sourceChannel = null; + FileChannel destChannel = null; + sourceChannel = new FileInputStream(source).getChannel(); + destChannel = new FileOutputStream(dest).getChannel(); + destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); + sourceChannel.close(); + destChannel.close(); + } + + public static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException { + FileUtils.copyFile(source, dest); + } + + public static void copyFileUsingJavaFiles(File source, File dest) throws IOException { + Files.copy(source.toPath(), dest.toPath()); + } + +} diff --git a/core-java/src/test/java/com/baeldung/stream/FileCopyTest.java b/core-java/src/test/java/com/baeldung/stream/FileCopyTest.java new file mode 100644 index 0000000000..3b915a3829 --- /dev/null +++ b/core-java/src/test/java/com/baeldung/stream/FileCopyTest.java @@ -0,0 +1,47 @@ +package com.baeldung.stream; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; + +import org.junit.Test; + +public class FileCopyTest { + + @Test + public void whenUsingStream_thenCopyFile() throws IOException { + File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_stream.txt"); + File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_stream.txt"); + FileCopy.copyFileUsingStream(src, dest); + assertTrue(dest.exists()); + dest.delete(); + } + + @Test + public void whenUsingFiles_thenCopyFile() throws IOException { + File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_files.txt"); + File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_files.txt"); + FileCopy.copyFileUsingJavaFiles(src, dest); + assertTrue(dest.exists()); + dest.delete(); + } + + @Test + public void whenUsingChannel_thenCopyFile() throws IOException { + File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_channel.txt"); + File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_channel.txt"); + FileCopy.copyFileUsingChannel(src, dest); + assertTrue(dest.exists()); + dest.delete(); + } + + @Test + public void whenUsingApache_thenCopyFile() throws IOException { + File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_apache.txt"); + File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_apache.txt"); + FileCopy.copyFileUsingApacheCommonsIO(src, dest); + assertTrue(dest.exists()); + dest.delete(); + } +} diff --git a/core-java/src/test/resources/copyTest/dest/readme.txt b/core-java/src/test/resources/copyTest/dest/readme.txt new file mode 100644 index 0000000000..dfda31ee9f --- /dev/null +++ b/core-java/src/test/resources/copyTest/dest/readme.txt @@ -0,0 +1,2 @@ +files will be copied here and then deleted +remove `file.delete()` to see the files here diff --git a/core-java/src/test/resources/copyTest/src/test_apache.txt b/core-java/src/test/resources/copyTest/src/test_apache.txt new file mode 100644 index 0000000000..a6b651973d --- /dev/null +++ b/core-java/src/test/resources/copyTest/src/test_apache.txt @@ -0,0 +1 @@ +apache diff --git a/core-java/src/test/resources/copyTest/src/test_channel.txt b/core-java/src/test/resources/copyTest/src/test_channel.txt new file mode 100644 index 0000000000..6dca835871 --- /dev/null +++ b/core-java/src/test/resources/copyTest/src/test_channel.txt @@ -0,0 +1 @@ +channel diff --git a/core-java/src/test/resources/copyTest/src/test_files.txt b/core-java/src/test/resources/copyTest/src/test_files.txt new file mode 100644 index 0000000000..027271b9b2 --- /dev/null +++ b/core-java/src/test/resources/copyTest/src/test_files.txt @@ -0,0 +1 @@ +files diff --git a/core-java/src/test/resources/copyTest/src/test_stream.txt b/core-java/src/test/resources/copyTest/src/test_stream.txt new file mode 100644 index 0000000000..eac9b41cbe --- /dev/null +++ b/core-java/src/test/resources/copyTest/src/test_stream.txt @@ -0,0 +1 @@ +stream