Skip to content

Commit a5f78cf

Browse files
afshar-mohammadpivovarit
authored andcommitted
BAEL-1308 - Copy a File with Java (eugenp#3000)
* Create Student.java * Create ApplicationContextTestBeanInjection.java * Update Student.java * added tests added tests * BAEL-1308 part 1 * Create test_stream.txt * Create test_files.txt * Create test_channel.txt * Create test_apache.txt * Create test.txt * Delete test.txt * Create readme.txt * Delete Student.java * Delete StudentInjectionTest.java * Delete ApplicationContextTestBeanInjection.java
1 parent bac07e4 commit a5f78cf

7 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.stream;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.OutputStream;
9+
import java.nio.channels.FileChannel;
10+
import java.nio.file.Files;
11+
12+
import org.apache.commons.io.FileUtils;
13+
14+
public class FileCopy {
15+
16+
public static void copyFileUsingStream(File source, File dest) throws IOException {
17+
InputStream is = null;
18+
OutputStream os = null;
19+
is = new FileInputStream(source);
20+
os = new FileOutputStream(dest);
21+
byte[] buffer = new byte[1024];
22+
int length;
23+
while ((length = is.read(buffer)) > 0) {
24+
os.write(buffer, 0, length);
25+
}
26+
is.close();
27+
os.close();
28+
}
29+
30+
public static void copyFileUsingChannel(File source, File dest) throws IOException {
31+
FileChannel sourceChannel = null;
32+
FileChannel destChannel = null;
33+
sourceChannel = new FileInputStream(source).getChannel();
34+
destChannel = new FileOutputStream(dest).getChannel();
35+
destChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
36+
sourceChannel.close();
37+
destChannel.close();
38+
}
39+
40+
public static void copyFileUsingApacheCommonsIO(File source, File dest) throws IOException {
41+
FileUtils.copyFile(source, dest);
42+
}
43+
44+
public static void copyFileUsingJavaFiles(File source, File dest) throws IOException {
45+
Files.copy(source.toPath(), dest.toPath());
46+
}
47+
48+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.baeldung.stream;
2+
3+
import static org.junit.Assert.assertTrue;
4+
5+
import java.io.File;
6+
import java.io.IOException;
7+
8+
import org.junit.Test;
9+
10+
public class FileCopyTest {
11+
12+
@Test
13+
public void whenUsingStream_thenCopyFile() throws IOException {
14+
File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_stream.txt");
15+
File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_stream.txt");
16+
FileCopy.copyFileUsingStream(src, dest);
17+
assertTrue(dest.exists());
18+
dest.delete();
19+
}
20+
21+
@Test
22+
public void whenUsingFiles_thenCopyFile() throws IOException {
23+
File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_files.txt");
24+
File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_files.txt");
25+
FileCopy.copyFileUsingJavaFiles(src, dest);
26+
assertTrue(dest.exists());
27+
dest.delete();
28+
}
29+
30+
@Test
31+
public void whenUsingChannel_thenCopyFile() throws IOException {
32+
File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_channel.txt");
33+
File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_channel.txt");
34+
FileCopy.copyFileUsingChannel(src, dest);
35+
assertTrue(dest.exists());
36+
dest.delete();
37+
}
38+
39+
@Test
40+
public void whenUsingApache_thenCopyFile() throws IOException {
41+
File src = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "src" + File.separator + "test_apache.txt");
42+
File dest = new File("src" + File.separator + "test" + File.separator + "resources" + File.separator + "copyTest" + File.separator + "dest" + File.separator + "test_apache.txt");
43+
FileCopy.copyFileUsingApacheCommonsIO(src, dest);
44+
assertTrue(dest.exists());
45+
dest.delete();
46+
}
47+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
files will be copied here and then deleted
2+
remove `file.delete()` to see the files here
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
apache
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
channel
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
files
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
stream

0 commit comments

Comments
 (0)