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

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

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

文件夹

最新文章

  1. sql查询
  2. [No00003F]richtextbox实现拖放
  3. [Asp.net MVC]Asp.net MVC5系列——添加模型
  4. 深入.NET内测题
  5. 记”Uri.IsWellFormedUriString”中的BUG
  6. WPF里面的常用笔刷
  7. GO:格式化代码
  8. History Grading
  9. [资料] Apache2 的 httpd.conf 经典中文翻译
  10. UVA507-- Jill Rides Again
  11. poj1679(最小生成树)
  12. L2-001. 紧急救援
  13. 对RabbitMQ.Client进行一下小小的包装,绝对实用方便
  14. 对python编程的初步理解
  15. DotNetCasClient 如何获取Cas服务器返回的attributes中的数据
  16. 小谈ConcurrentHashMap
  17. C# 获取系统当前IE版本号
  18. [server]阿里云服务器远程文件传输的解决方案
  19. ubuntu中文乱码解决
  20. 「POJ-3608」Bridge Across Islands (旋转卡壳--求两凸包距离)

热门文章

  1. Python 2.7.9 Demo - 020.函数的定义、返回
  2. 轮询、长轮询、长连接、websocket
  3. 0058 Spring MVC如何向视图传值--Model--ModelMap--ModelAndView--@ModelAttribute
  4. spring使用rssfeed
  5. Java类型Float&&Double
  6. spring 在service中需要抛出异常才能自动回滚
  7. hdu 1007最近点对问题
  8. 服务器响应慢的分析与解决(Linux服务器)
  9. 【Unity笔记】摄像机、图片的模糊处理
  10. 周末大礼:jQuery技巧总结