-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBufferToText.java
More file actions
54 lines (47 loc) · 1.84 KB
/
Copy pathBufferToText.java
File metadata and controls
54 lines (47 loc) · 1.84 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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
public class BufferToText {
private static final int BSIZE = 1024;
public static void main(String[] args) throws IOException {
String source = "source.data";
String dest = "dest.data";
FileChannel channel = new FileOutputStream(source).getChannel();
channel.write(ByteBuffer.wrap("someText".getBytes()));
channel.close();
channel = new FileInputStream(source).getChannel();
ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
channel.read(buffer);
buffer.flip();
System.out.println(buffer.asCharBuffer());
//decode
buffer.rewind();
String encode = System.getProperty("file.encoding");
System.out.println("decode String " + encode + " " + Charset.forName(encode).decode(buffer));
channel = new FileOutputStream(source).getChannel();
channel.write(ByteBuffer.wrap("Some txt ".getBytes("UTF-16BE")));
channel.close();
//Now try reading again
buffer.clear();
channel = new FileInputStream(source).getChannel();
channel.read(buffer);
buffer.flip();
System.out.println(buffer.asCharBuffer());
//Use a CharBuffer to write through
channel = new FileOutputStream(source).getChannel();
buffer = ByteBuffer.allocate(24);
buffer.asCharBuffer().put("some text");
channel.write(buffer);
channel.close();
//read and display
channel = new FileInputStream(source).getChannel();
buffer.clear();
channel.read(buffer);
buffer.flip();
System.out.println(buffer.asCharBuffer());
}
}