« | October 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | 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 | | |
| 公告 |
不知不觉6年了
|
Blog信息 |
blog名称:〾堯仸〾的天空 日志总数:139 评论数量:503 留言数量:16 访问次数:2720364 建立时间:2005年4月23日 |

| |
[java]BufferToText 文章收藏, 随笔, 软件技术
〾堯仸〾 发表于 2009/4/3 15:15:37 |
btw : 冒个泡 。在看 thinking in java 3 package thinkinjava.c12;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 { FileChannel fc = new FileOutputStream("data2.txt").getChannel(); fc.write(ByteBuffer.wrap("Some text".getBytes() )); fc.close(); fc = new FileInputStream("data2.txt").getChannel(); ByteBuffer buff = ByteBuffer.allocate(BSIZE); fc.read(buff); buff.flip(); System.out.println(buff.asCharBuffer() ); //Decode using this system's default Charset buff.rewind(); String encoding = System.getProperty("file.encoding"); System.out.println("Decoded using " + encoding + Charset.forName(encoding).decode(buff)); //Or ,we could encode with someing that will print fc = new FileOutputStream("data2.txt").getChannel(); fc.write(ByteBuffer.wrap("some text".getBytes("UTF-16BE"))); fc.close(); fc = new FileInputStream("data2.txt").getChannel(); buff.clear(); fc.read(buff); buff.flip(); System.out.println(buff.asCharBuffer() ); /* 卯浥?數 Decoded using GBKSome text some text*/ }} |
|
|