从头认识java-16.5 nio的数据转换

这一章节我们来讨论一些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和编码的控制。

 


 

这一章节就到这里,谢谢。

-----------------------------------

目录



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值