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

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

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

文件夹

从头认识java-16.5 nio的数据转换的更多相关文章

  1. JAVA中的NIO (New IO)

    简介 标准的IO是基于字节流和字符流进行操作的,而JAVA中的NIO是基于Channel和Buffer进行操作的. 传统IO graph TB; 字节流 --> InputStream; 字节流 ...

  2. Java BIO、NIO与AIO的介绍(学习过程)

    Java BIO.NIO与AIO的介绍 因为netty是一个NIO的框架,所以在学习netty的过程中,开始之前.针对于BIO,NIO,AIO进行一个完整的学习. 学习资源分享: Netty学习:ht ...

  3. 官方正式发布 Java 16

    前言 就在2021/03/16,官方正式发布了Java 16.我们可以下载使用Java 16了. 特性 向量API(孵化) 在运行期,Vector 表示向量计算可以可靠地编译成支持CPU架构上的最佳矢 ...

  4. Java 16 新功能介绍

    点赞再看,动力无限.Hello world : ) 微信搜「程序猿阿朗 」. 本文 Github.com/niumoo/JavaNotes 和 程序猿阿朗博客 已经收录,有很多知识点和系列文章. Ja ...

  5. Java Socket(3): NIO

    NIO采取通道(Channel)和缓冲区(Buffer)来传输和保存数据,它是非阻塞式的I/O,即在等待连接.读写数据(这些都是在一线程以客户端的程序中会阻塞线程的操作)的时候,程序也可以做其他事情, ...

  6. 【JAVA】【NIO】3、Java NIO Channel

    Java NIO和流量相似,但有些差异: ·通道可读写,流仅支持单向.读或写 ·异步通道读取 ·通道读写器,他们是和Buffer交替 道的实现 下面是Java NIO中最重要的通道的实现: ·File ...

  7. 【JAVA】【NIO】5、Java NIO Scatter / Gather

    标题手段Java NIO该分散体浓缩 Java NIO内置支持分散与收集.的概念主要用于信道分散聚集的读写. 读出的分散体的一个通道被读多个数据buffer在.因此.数据分散到多个buffer中. 对 ...

  8. Java中的NIO基础知识

    上一篇介绍了五种NIO模型,本篇将介绍Java中的NIO类库,为学习netty做好铺垫 Java NIO 由3个核心组成,分别是Channels,Buffers,Selectors.本文主要介绍着三个 ...

  9. Java的BIO,NIO和AIO的区别于演进

    作者:公众号:我是攻城师 前言 Java里面的IO模型种类较多,主要包括BIO,NIO和AIO,每个IO模型都有不一样的地方,那么这些IO模型是如何演变呢,底层的原理又是怎样的呢? 本文我们就来聊聊. ...

随机推荐

  1. cvs history,CVS中查询目录下所有文件的提交记录

    我们习惯用eclipse svn中查看一个目录下,甚至整个工程下,所有的文件的变更列表:操作为:右键工程 -> Team -> Show History. 最近在使用CVS,eclipse ...

  2. 深入理解Eureka - Eureka配置列表

    Eureka包含四个部分的配置 instance:当前Eureka Instance实例信息配置 client:Eureka Client客户端特性配置 server:Eureka Server注册中 ...

  3. Logstash5.3借助临时字段修改@timestamp为北京时间,方便按天生成output文件

    $more config/first-pipeline.conf input { beats { port => " } } filter { if [type] == "s ...

  4. Error reading field 'throttle_time_ms': java.nio.BufferUnderflowException

    可能出现的问题: ERROR o.a.k.c.p.i.Sender – Uncaught error in kafka producer I/O thread: org.apache.kafka.co ...

  5. 为WPF程序添加字体

    很多时候我们开发的程序可能会在多个版本的Windows上运行,比如XP.Win7.Win8. 为了程序美观,现在很多公司会使用WPF作为程序的界面设计. 跨版本的操作的操作系统往往有一些字体上的问题, ...

  6. C语言 · 核桃的数量

    历届试题 核桃的数量   时间限制:1.0s   内存限制:256.0MB        锦囊1 最小公倍数. 锦囊2 答案是a, b, c的最小公倍数.   问题描述 小张是软件项目经理,他带领3个 ...

  7. Hadoop Balancer源代码解读

    前言 近期在做一些Hadoop运维的相关工作,发现了一个有趣的问题,我们公司的Hadoop集群磁盘占比数值參差不齐,高的接近80%.低的接近40%.并没有充分利用好上面的资源,可是balance的操作 ...

  8. es 配置文件

    [root@es02 config]# egrep -v "^(#|$)" elasticsearch.yml cluster.name: v5-applicationnode.n ...

  9. FastDFS-单机版安装

    转载自: 搭建单机版的FastDFS服务器 * 为了便于理解,其中顺序有改变. 1.第八步创建软链接,可以等到第九步结束后进行.如果提前在第八步创建软链接,因为还没有安装 libfdfsclient. ...

  10. static、extern分析

    1.extern extern在变量声明中有这样一个作用:你要在demo2.cpp中引用demo1.cpp中的一个全局变量,就要在demo2.h中用extern来声明这个全局变量(或者在demo1.h ...