Repository navigation
Expand file tree
/
Copy pathBufferToText.java
More file actions
51 lines (42 loc) · 1.7 KB
/
Copy pathBufferToText.java
File metadata and controls
51 lines (42 loc) · 1.7 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
package btp.oneP;
import java.io.FileInputStream;
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 {
FileChannel fc = new FileOutputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
fc.write(ByteBuffer.wrap("some text".getBytes()));
fc.close();
fc = new FileInputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
fc.read(buff);
buff.flip();
System.out.println(buff.asCharBuffer());
buff.rewind();
String encoding = System.getProperty("file.encoding");
System.out.println("Decoded using "+encoding+":"+Charset.forName(encoding).decode(buff));
fc = new FileOutputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
fc.write(ByteBuffer.wrap("Some text".getBytes("GBK")));
fc.close();
fc = new FileInputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
buff.clear();
fc.read(buff);
buff.flip();
System.out.println(buff.asCharBuffer());
System.out.println("Decoded using "+"GBK"+":"+Charset.forName("GBK").decode(buff));
fc = new FileOutputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
buff = ByteBuffer.allocate(24);
buff.asCharBuffer().put("Some text!");
fc.write(buff);
fc.close();
fc = new FileInputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
buff.clear();
fc.read(buff);
buff.flip();
System.out.println(buff.asCharBuffer());
}
}