forked from TimSongCoder/LearnJavaForAndroid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileChannelDemo.java
More file actions
37 lines (34 loc) · 1.4 KB
/
Copy pathFileChannelDemo.java
File metadata and controls
37 lines (34 loc) · 1.4 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
import java.io.RandomAccessFile;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
public class FileChannelDemo{
public static void main(String[] args) throws IOException{
RandomAccessFile file = new RandomAccessFile("FileChannelDemo.temp", "rw");
FileChannel fileChannel = file.getChannel();
long pos;
System.out.println("Underlying file position: " + (pos = fileChannel.position())
+ ", file size: " + fileChannel.size());
String msg = null;
if(args.length > 0){
msg = args[0];
}else{
msg = "This is a demo message for test.";
}
ByteBuffer buffer = ByteBuffer.allocateDirect(msg.length()*2); // char is two byte long.
buffer.asCharBuffer().put(msg); // CharBuffer used as a view buffer, its change is visible to the backing ByteBuffer.
fileChannel.write(buffer);
// recommend the operating system to commit the written data to storage device, including metadata.
fileChannel.force(true);
System.out.println("Underlying file position: " + fileChannel.position()
+ ", file size: " + fileChannel.size());
buffer.clear(); // prepare for refresh use as newly created buffer.
fileChannel.position(pos); // prepare for read operation
fileChannel.read(buffer);
// Inspect the buffer data after read.
buffer.flip();
while(buffer.hasRemaining()){
System.out.print(buffer.getChar()); // retrieve 2 bytes per time.
}
}
}