从头认识java-16.5 nio的数据转换
来源:互联网 发布:淘宝卖教程选什么类目 编辑:程序博客网 时间:2024/05/20 16:42
这一章节我们来讨论一些nio的数据转换。
上面一章节我们提到ByteBuffer,但是他是面向二进制数据,对于编程来说不是很方便,因此,java增加了转换数据的工具。
1.asCharBuffer
package com.ray.ch16;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;public class Test {public static void main(String[] args) throws IOException {String path = "d://123.txt";FileOutputStream fileOutputStream = new FileOutputStream(path);FileChannel fileChannel = fileOutputStream.getChannel();ByteBuffer buffer = ByteBuffer.allocate(24);buffer.asCharBuffer().put("welcome");//在这里可以方便的使用String就可以把元素放到缓冲区while (buffer.hasRemaining()) {fileChannel.write(buffer);}fileChannel.close();FileInputStream fileInputStream = new FileInputStream(path);fileChannel = fileInputStream.getChannel();fileChannel.read(buffer);buffer.flip();System.out.println(buffer.asCharBuffer());fileChannel.close();}}
输出:
welcome(其实后面还有一些乱码,这里显示不出来)
注意:这里的乱码其实是剩余的字符,welcome使用了14个字符,剩余的10个字符也会产生,然后打印出来, 我们在具体的文件里面就可以看见,welcome后面还跟着一堆空格。
2.控制输出输入的编码
package com.ray.ch14;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.nio.ByteBuffer;import java.nio.channels.FileChannel;public class Test {public static void main(String[] args) throws IOException {String path = "d://123.txt";FileOutputStream fileOutputStream = new FileOutputStream(path);FileChannel fileChannel = fileOutputStream.getChannel();ByteBuffer buffer = ByteBuffer.wrap("hello world".getBytes("UTF-8"));//控制输入的编码fileChannel.write(buffer);fileChannel.close();fileOutputStream.close();System.out.println(System.getProperty("file.encoding"));FileInputStream fileInputStream = new FileInputStream(path);fileChannel = fileInputStream.getChannel();fileChannel.read(buffer);buffer.flip();System.out.println(buffer.asCharBuffer());fileChannel.close();fileInputStream.close();}}
输出:
GBK
桥汬漠睯牬
由于我当前的编码是GBK,因此输出的时候发生乱码。
下面是我修改过的代码:
package com.ray.ch14;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 Test {public static void main(String[] args) throws IOException {String path = "d://123.txt";FileOutputStream fileOutputStream = new FileOutputStream(path);FileChannel fileChannel = fileOutputStream.getChannel();ByteBuffer buffer = ByteBuffer.wrap("hello world".getBytes("UTF-8"));fileChannel.write(buffer);fileChannel.close();fileOutputStream.close();System.out.println(System.getProperty("file.encoding"));FileInputStream fileInputStream = new FileInputStream(path);fileChannel = fileInputStream.getChannel();fileChannel.read(buffer);buffer.flip();String encoding = "UTF-8";System.out.println(Charset.forName(encoding).decode(buffer));//这里使用跟上面相同的编码解码fileChannel.close();fileInputStream.close();}}
输出:
GBK
hello world
哪怕我的默认编码是GBK,但是也可以解码出原来的字符串。
总结:这一章节介绍nio的asCharBuffer和编码的控制。
这一章节就到这里,谢谢。
-----------------------------------
目录
0 0
- 从头认识java-16.5 nio的数据转换
- 从头认识java-16.4 nio的读与写(ByteBuffer的使用)
- 从头认识java-4.6 成员的初始化
- 从头认识java-11.1 不变的字符串
- 从头认识java-13.10 边界的作用
- 从头认识java-目录
- 从头认识java-9.3 向容器添加一组数据与容器的打印
- 认识java nio
- 从头认识java-1.6 多态
- 从头认识java-4.4 this
- 从头认识java-7.2 接口
- 从头认识java-9.7 LinkedList
- 从头认识java-9.9 set
- 从头认识java-9.10 Map
- 从头认识java-9.11 Queue
- 从头认识java-17.1 多线程
- 从头认识java-4.5 对象的清理(finalize())
- 从头认识java-4.8 数组的初始化(1)
- php写的可以在本机发送iOS push程序
- 808 协议的解析
- CentOS 6.X 关闭不需要的 TTY 方法
- Meteor:关闭所有tab时退出登录
- STL中hashtable,hashset,hashmap,set,map,multiset和multimap的区别
- 从头认识java-16.5 nio的数据转换
- 绑定服务的初步解读
- logback学习
- Undefined symbols for architecture armv7问题
- Word自动目录字体过大,如何全选并修改样式
- 结构体初始化
- php模块memcache和memcached区别分析
- 离散--繁琐的定义
- linux下的stdin,stdout和stderr理解