nio FileChannel中文乱码问题
来源:互联网 发布:php是什么货币单位 编辑:程序博客网 时间:2024/06/05 19:27
http://www.voidcn.com/blog/chuyouyinghe/article/p-5979076.html
最近用nio读取文件时,英文正常,读取中文时会出现乱码,经查可以用Charset类来解决:
代码如下:
package com.example.demo;import java.io.FileNotFoundException;import java.io.RandomAccessFile;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 NioDemo {public static void main(String[] args) {try {Charset charset = Charset.forName("GBK");//Java.nio.charset.Charset处理了字符转换问题。它通过构造CharsetEncoder和CharsetDecoder将字符序列转换成字节和逆转换。 CharsetDecoder decoder = charset.newDecoder();RandomAccessFile raf = new RandomAccessFile("D:\\d\\test.txt", "rw");FileChannel fc = raf.getChannel();ByteBuffer buffer = ByteBuffer.allocate(512); CharBuffer cb = CharBuffer.allocate(512);int count = fc.read(buffer);while (count != -1) { System.out.println("count = "+count); buffer.flip(); decoder.decode(buffer, cb, false); cb.flip(); while (cb.hasRemaining()) {System.out.print(cb.get());} System.out.println();buffer.clear();cb.clear();count = fc.read(buffer);}raf.close();} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
test..txt文体如下:据英国《每日快报》报道,爱因斯坦提出理论称,通过时间旅行前往未来的一种方式就是在宇宙中以光速旅行。这个广泛接受的理论称,实现时间旅行必须打造一艘能够以光速飞行的太空船,并且进入太空。理论物理学家、哥伦比亚大学的briangreene称:“你可以打造出一艘宇宙飞船,进入太空并且以接近光速的速度旅行,转个圈然后回来。设想一下你飞行了六个月,然后掉头再飞六个月回来。”
运行结果:
package com.lp.liuan;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;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 FileChannelExample { public static void main(String args[]){ Charset charset = Charset.forName("GBK");//Java.nio.charset.Charset处理了字符转换问题。它通过构造CharsetEncoder和CharsetDecoder将字符序列转换成字节和逆转换。 CharsetDecoder decoder = charset.newDecoder(); FileInputStream fis = null; try { fis = new FileInputStream("E:/学习总结/java乱码本质.txt"); FileChannel fileChannel = fis.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(1024); CharBuffer charBuffer = CharBuffer.allocate(1024); int bytes = fileChannel.read(byteBuffer); while(bytes!=-1){ byteBuffer.flip(); decoder.decode(byteBuffer, charBuffer, false); charBuffer.flip(); System.out.println(charBuffer); charBuffer.clear(); byteBuffer.clear(); bytes = fileChannel.read(byteBuffer); } if(fis!=null){ fis.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }}
阅读全文
0 0
- nio FileChannel中文乱码问题
- nio FileChannel中文乱码问题
- NIO - FileChannel
- NIO - FileChannel
- NIO - FileChannel
- nio filechannel
- Nio-FileChannel与粘包问题
- 【NIO】通过FileChannel读取包含中文内容的文件
- NIO 中文乱码问题的解决代码实现
- Java NIO FileChannel
- JAVA NIO之FileChannel
- Java NIO(8-FileChannel)
- Java nio 之 FileChannel
- Java NIO FileChannel
- Java NIO FileChannel
- java nio(二)FileChannel
- Java NIO -- FileChannel
- Java NIO (七) FileChannel
- 初识nodejs15
- 设置TOMCAT启用GZIP压缩 包括导致没返回的问题
- Noip2012 Day1 T3 开车旅行无题解的70分代码(未完成)
- JLRoutes
- Apache Spark 内存管理详解
- nio FileChannel中文乱码问题
- 51nod【1344】走格子【水题】
- acm 模板
- 提高PHP效率,不能不看的50个细节
- SparkSQL---开窗函数(java)
- 欢迎使用CSDN-markdown编辑器
- 怎样查看win10下Python安装路径?
- POJ 1195 Mobile phones(二维树状数组)
- 【学习笔记】C# 接口