-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathByteStreamCopyExample.java
More file actions
71 lines (57 loc) · 2.35 KB
/
Copy pathByteStreamCopyExample.java
File metadata and controls
71 lines (57 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
* Day 22 - File Handling: Byte Streams
*
* <p>Demonstrates copying a file using {@link java.io.FileInputStream} and
* {@link java.io.FileOutputStream}.
*
* <p>This example creates a small input file, copies it, and verifies the copy.
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class ByteStreamCopyExample {
private static void writeBytes(String path, byte[] data) throws IOException {
try (FileOutputStream out = new FileOutputStream(path)) {
out.write(data);
}
}
private static byte[] readAllBytes(String path) throws IOException {
try (FileInputStream in = new FileInputStream(path)) {
byte[] buffer = new byte[1024];
int read;
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
while ((read = in.read(buffer)) != -1) {
baos.write(buffer, 0, read);
}
return baos.toByteArray();
}
}
private static void copy(String source, String target) throws IOException {
try (FileInputStream in = new FileInputStream(source);
FileOutputStream out = new FileOutputStream(target)) {
byte[] buffer = new byte[256];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
}
public static void main(String[] args) throws IOException {
System.out.println("=== Byte Stream Copy Example ===\n");
String source = "byte-input.bin";
String target = "byte-copy.bin";
byte[] payload = ("Binary-like content\n" +
"This file was created by ByteStreamCopyExample\n")
.getBytes(StandardCharsets.UTF_8);
System.out.println("Creating source file: " + source);
writeBytes(source, payload);
System.out.println("Copying to: " + target);
copy(source, target);
byte[] srcBytes = readAllBytes(source);
byte[] dstBytes = readAllBytes(target);
System.out.println("Source size: " + srcBytes.length + " bytes");
System.out.println("Copy size: " + dstBytes.length + " bytes");
System.out.println("Copy matches source: " + java.util.Arrays.equals(srcBytes, dstBytes));
}
}