【NIO】通过FileChannel读取包含中文内容的文件
来源:互联网 发布:js syntax error 编辑:程序博客网 时间:2024/06/08 00:56
直接上代码:
package com.xx.nio;import java.io.File;import java.io.FileInputStream;import java.nio.ByteBuffer;import java.nio.CharBuffer;import java.nio.channels.FileChannel;import java.nio.charset.Charset;import java.nio.charset.CharsetDecoder;public class ChannelTest {public static void main(String[] args) throws Exception {String path = ChannelTest.class.getResource("/").getPath();FileInputStream fis = new FileInputStream(new File(path + "nio-test.txt"));FileChannel channel = fis.getChannel();/* * Java.nio.charset.Charset处理了字符转换问题。 * 它通过构造CharsetEncoder和CharsetDecoder将字符序列转换成字节和逆转换。 */Charset charset = Charset.forName("UTF-8");CharsetDecoder decoder = charset.newDecoder();ByteBuffer buffer = ByteBuffer.allocate(128);CharBuffer charBuffer = CharBuffer.allocate(128);int i = channel.read(buffer);while(i != -1){buffer.flip(); // 切换到读模式decoder.decode(buffer, charBuffer, false);charBuffer.flip(); // 切换到读模式while(charBuffer.hasRemaining()){char c = charBuffer.get();System.out.print(c);}charBuffer.clear();buffer.clear();i = channel.read(buffer);}channel.close();fis.close();}}
0 0
- 【NIO】通过FileChannel读取包含中文内容的文件
- 使用NIO的FileChannel和ByteBuffer高效读取文件
- nio FileChannel中文乱码问题
- nio FileChannel中文乱码问题
- 使用FileChannel读取文件
- 通过fileChannel复制文件
- 读取C# Textbox控件中包含中文内容的长度
- 读取C# Textbox控件中包含中文内容的长度
- 读取中文文件内容的测试
- qt读取中文文件内容的测试
- 读取中文文件内容的测试
- Java的NIO之FileChannel
- 利用filechannel拷贝文件内容
- NIO - FileChannel
- NIO - FileChannel
- NIO - FileChannel
- nio filechannel
- 通过URL读取文件内容
- 记录下发现神书,感觉自己的python水平又要上一个台阶了
- CodeForces 301B Yaroslav and Time
- 1.简介以及环境搭建
- 2.python中文编码
- windows下好用软件:closeADs+everything+Ditto
- 【NIO】通过FileChannel读取包含中文内容的文件
- Delaunay三角剖分(Delaunay Triangulation)概述
- 3.基础语法
- SQL延伸2-最简单的行列互换
- Git查看、删除、重命名远程分支和tag
- 4.变量、数据类型
- 通过RTCP进行媒体流实时丢包重传的方法
- 5.运算符
- 【SSLGZ 2654】【TE】糊涂的教授