从头认识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