forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileChannelEx.java
More file actions
35 lines (23 loc) · 844 Bytes
/
FileChannelEx.java
File metadata and controls
35 lines (23 loc) · 844 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
package com.zetcode;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class FileChannelEx {
public static void main(String[] args) throws IOException {
var fileName = "src/resources/thermopylae.txt";
try (RandomAccessFile myFile = new RandomAccessFile(fileName, "rw");
FileChannel inChannel = myFile.getChannel()) {
ByteBuffer buf = ByteBuffer.allocate(48);
int bytesRead = inChannel.read(buf);
while (bytesRead != -1) {
buf.flip();
while (buf.hasRemaining()) {
System.out.print((char) buf.get());
}
buf.clear();
bytesRead = inChannel.read(buf);
}
}
}
}