forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSevenByteBufferChannel.java
More file actions
37 lines (25 loc) · 946 Bytes
/
SevenByteBufferChannel.java
File metadata and controls
37 lines (25 loc) · 946 Bytes
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
package com.zetcode;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.Channels;
import java.nio.file.Files;
import java.nio.file.Paths;
public class SevenByteBufferChannel {
public static void main(String[] args) throws IOException {
var srcFile = Paths.get("src/resources/data.txt");
var destFile = Paths.get("src/resources/data2.txt");
try (var src = Channels.newChannel(Files.newInputStream(srcFile))) {
try (var dest = Channels.newChannel(Files.newOutputStream(destFile))) {
var buf = ByteBuffer.allocate(7);
while (src.read(buf) != -1) {
// flips the buffer from read to write
buf.flip();
while (buf.hasRemaining()) {
dest.write(buf);
}
buf.clear();
}
}
}
}
}