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

时间:2022-09-03 12:19:57

这一章节我们来讨论一些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. Development of large-scale site performance optimization method from LiveJournal background

    A LiveJournal course of development is a project in the 99 years began in the campus, a few people d ...

  2. Compiler Theory(编译原理)、词法/语法/AST/中间代码优化在Webshell检测上的应用

    catalog . 引论 . 构建一个编译器的相关科学 . 程序设计语言基础 . 一个简单的语法制导翻译器 . 简单表达式的翻译器(源代码示例) . 词法分析 . 生成中间代码 . 词法分析器的实现 ...

  3. 洛谷1352 CODEVS1380 没有上司的舞会

    洛谷的测试数据貌似有问题,4个点RE不可避 CODEVS可AC —————— 10分钟后追记:在洛谷把数组范围开到10000+就过了 —————— 题目描述 Description Ural大学有N个 ...

  4. FAQ系列 | 解读EXPLAIN执行计划中的key_len

    http://imysql.com/2015/10/20/mysql-faq-key-len-in-explain.shtml

  5. SNS团队第二次站立会议(2017.04.23)

    一.当天站立式会议照片 本次会议主要内容:汇报工作进度,根据完成情况调整进度 二.每个人的工作 成员 今天已完成的工作 明天计划完成的工作 罗于婕 梳理清楚数据的每个类型和数据项  具体落实把相关数据 ...

  6. grep -vFf 比较2个文件差异

    grep -vFf 1.txt 2.txt   打印出2.txt中与1.txt有差异的数据. #cat .txt 192.168.0.1 192.168.0.2 192.168.0.3 #cat .t ...

  7. C# MethodInvoker委托的使用

    一.MethodInvoker是什么? MethodInvoker 表示一个委托,该委托可以执行托管代码中声明为void且不接受任何参数的任何方法.在对控件的 invoke 方法进行调用时或需要一个简 ...

  8. failed to find global analyzer [uax_url_email]

    ES的默认分词设置是standard,这个在中文分词时就比较尴尬了,会单字拆分,比如我搜索关键词“清华大学”,这时候会按“清”,“华”,“大”,“学”去分词,然后搜出来的都是些“清清的河水”,“中华儿 ...

  9. java安全删除一个文件,防止工具恢复数据

    解决移动端文件删除的安全问题:file.delect()   Java 确保安全删除某个文件 http://outofmemory.cn/code-snippet/14222/Java-securit ...

  10. 解决Visio复制绘图时虚框变实框的问题

    参考:http://www.educity.cn/help/653700.html 问题好像是,在VISIO里只要虚线框的大小超过一个界限,拷贝之后就会变成实线框. 解决办法是修改注册表:[运行reg ...