nio基本使用一
来源:互联网 发布:手机mac地址不可用 编辑:程序博客网 时间:2024/06/11 08:24
CharBuffer有一个toString()方法是这样定义的:“返回一个包含缓冲器中所有字符的字符串”。
public class BufferToText { private static final int BSIZE = 1024; public static void main(String[] args) throws Exception { FileChannel fc = new FileOutputStream("data3.txt").getChannel(); fc.write(ByteBuffer.wrap("Some text".getBytes())); fc.close(); fc = new FileInputStream("data3.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("Deconded using " + encoding + ":" + Charset.forName(encoding).decode(buff));//按系统输入时的格式解码 fc = new FileOutputStream("data3.txt").getChannel(); fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE")));//输入时对其进行编码 fc.close(); fc = new FileInputStream("data3.txt").getChannel(); buff.clear(); fc.read(buff); buff.flip(); System.out.println(buff.asCharBuffer()); fc = new FileOutputStream("data3.txt").getChannel(); buff = ByteBuffer.allocate(500); buff.asCharBuffer().put("Some text");//转化成charBuffer再输入 fc.write(buff); fc.close(); fc = new FileInputStream("data3.txt").getChannel(); buff.clear(); fc.read(buff); buff.flip(); System.out.println(buff.asCharBuffer()); }}缓冲器荣啦的是普通的字节,为了把它们转化为字符串,我们要么在输入它们的时候对其进行编码,要么在将其从缓冲器输出时对它们进行解码。
尽管ByteBuffer只能保存字节类型的数据,但是它具有可以从其所容纳的字节中产生出各种不同基本类型值的方法。
public class GetData { private static final int BSIZE = 1024; public static void main(String[] args) { ByteBuffer bb = ByteBuffer.allocate(BSIZE); int i = 0; while (i++ < bb.limit()) if (bb.get() != 0) System.out.print("nonzero"); System.out.println("i = " + i); bb.rewind(); bb.asCharBuffer().put("Howdy!"); char c; while ((c = bb.getChar()) != 0) System.out.print(c + " "); System.out.println(); bb.rewind(); bb.asShortBuffer().put((short) 471142); System.out.println(bb.getShort()); bb.rewind(); bb.asIntBuffer().put(99471142); System.out.println(bb.getInt()); bb.rewind(); bb.asLongBuffer().put(99471142); System.out.println(bb.getLong()); bb.rewind(); bb.asFloatBuffer().put(99471142); System.out.println(bb.getFloat()); bb.rewind(); bb.asDoubleBuffer().put(99471142); System.out.println(bb.getDouble()); bb.rewind(); }}
会映射成为对ByteBuffer中数据的修改。
public class IntBufferDemo { private static final int BSIZE= 1024; public static void main(String[] args){ ByteBuffer bb=ByteBuffer.allocate(BSIZE); IntBuffer ib=bb.asIntBuffer(); ib.put(new int[]{11,42,47,99,143,811,1016}); System.out.println(ib.get(3)); ib.put(3,1811); ib.flip(); while (ib.hasRemaining()){ int i=ib.get(); System.out.println(i); } }}
阅读全文
0 0
- nio基本使用一
- Java NIO基本使用
- Java NIO基本使用
- Nio基本使用
- nio基本使用
- nio/mina(一) nio基本通信
- NIO学习二、NIO的基本使用
- netty使用(一):java NIO
- NIO(一)--Path的使用
- java.nio --Path--Files--基本使用
- Java NIO使用及原理分析 (一)
- Java NIO使用及原理分析 (一)
- Java NIO使用及原理分析 (一)
- Java NIO使用及原理分析 (一)
- Java NIO使用及原理分析 (一)
- Java NIO使用及原理分析 (一)
- Java NIO使用及原理分析 (一)
- Java NIO使用及原理分析 (一)
- VTK图形处理之剪裁
- 2017 ACM-ICPC 亚洲区(南宁赛区)网络赛 H. A Cache Simulator
- 启动plsql登录界面很慢,要大概十分钟才显示,但是一打开进程是有的
- leetcode_链表总结
- 第四周 项目三 单链表应用 1.
- nio基本使用一
- EasyNVR无插件直播服务器如何使用ffmpeg实现摄像机快照功能的
- HashMap实现原理
- Spring Security3.1登陆验证
- setText()的一个细节导致android.content.res.Resources$NotFoundException: String resource ID #0x7
- Git入门介绍-5-Tags-入门结束篇
- Qt学习笔记:信号和槽机制
- linux软件安装——RPM与YUM
- react native学习笔记10——react-navigation的嵌套使用