用NIO去读中文文件,出现乱码现象,请前辈们帮忙看一下问题,注:byteBuffer不能一定装下文件的所有内容,需要循环去读。测试文件:
我爱itexample.com,今天是个周末。
测试用例:package com.itexample.nio;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;public class ReadFile { public static void main(String[] args) throws IOException {
readFile("C:/Users/limaoyuan/Desktop/测试nio.txt");
// 用apache的io包读就没问题,但这个io包也没有用到nio的技术
// String content = FileUtils.readFileToString(new File("C:/Users/limaoyuan/Desktop//测试nio.txt"),Charset.forName("GBK"));
// System.out.println(content);
}
private static void readFile(String filepath) throws IOException{
File file = new File(filepath);
if(file.exists()){
Charset charset = Charset.forName("GBK");// 创建GBK字符集
FileInputStream fin = new FileInputStream(file);
FileChannel fcIn = fin.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
while(fcIn.read(byteBuffer) != -1){
byteBuffer.flip();
System.out.print(charset.decode(byteBuffer));
byteBuffer.clear();
}
}
}
}我爱itexample.com,?裉焓歉鲋苣?
有乱码现象,如果把代码中
ByteBuffer byteBuffer = ByteBuffer.allocate(10); 改为 ByteBuffer byteBuffer = ByteBuffer.allocate(100);
那么就能正确输出,看来问题就在于1个中文需要4个字节,缓存到ByteBuffer中的字节把一个中文字分成了多段来转换,那肯定会有问题了,可是我已经使用了nio自己提供的Charset类来作转换了,可还是会乱码。Charset charset = Charset.forName("GBK");//来定义字符为GBK了
System.out.print(charset.decode(byteBuffer));
可还是会乱码,谁能知道请告知一二,谢谢。转载请注明:itexample » java nio中文乱码问题
http://www.itexample.com/java/java-nio-charset/java nio乱码
我爱itexample.com,今天是个周末。
测试用例:package com.itexample.nio;import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;public class ReadFile { public static void main(String[] args) throws IOException {
readFile("C:/Users/limaoyuan/Desktop/测试nio.txt");
// 用apache的io包读就没问题,但这个io包也没有用到nio的技术
// String content = FileUtils.readFileToString(new File("C:/Users/limaoyuan/Desktop//测试nio.txt"),Charset.forName("GBK"));
// System.out.println(content);
}
private static void readFile(String filepath) throws IOException{
File file = new File(filepath);
if(file.exists()){
Charset charset = Charset.forName("GBK");// 创建GBK字符集
FileInputStream fin = new FileInputStream(file);
FileChannel fcIn = fin.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate(10);
while(fcIn.read(byteBuffer) != -1){
byteBuffer.flip();
System.out.print(charset.decode(byteBuffer));
byteBuffer.clear();
}
}
}
}我爱itexample.com,?裉焓歉鲋苣?
有乱码现象,如果把代码中
ByteBuffer byteBuffer = ByteBuffer.allocate(10); 改为 ByteBuffer byteBuffer = ByteBuffer.allocate(100);
那么就能正确输出,看来问题就在于1个中文需要4个字节,缓存到ByteBuffer中的字节把一个中文字分成了多段来转换,那肯定会有问题了,可是我已经使用了nio自己提供的Charset类来作转换了,可还是会乱码。Charset charset = Charset.forName("GBK");//来定义字符为GBK了
System.out.print(charset.decode(byteBuffer));
可还是会乱码,谁能知道请告知一二,谢谢。转载请注明:itexample » java nio中文乱码问题
http://www.itexample.com/java/java-nio-charset/java nio乱码
你把逗号改成中文的啊,你1个字节1哥字节的读,但是你英文的个数是基数个,今天的今有一半先被读走了。。必然乱码啊。
Files和Paths是哪个包的?能否告诉一下?
问题产生的原因已经明了了,能知道如何在JDK6下如何解决这种问题吗?
谢谢,如果是JDK6有没有对应的办法解决?另外,您提供的这个方法,如果文章特别大,等于还是要循环一次都加到内存中,然后再一行一行的显示出来,有没有边读边显示的方法在JDK7中,用NIO的方式,谢谢。