读书人

java中读取各种资料编码的文本文件并正

发布时间: 2012-09-01 09:33:02 作者: rapoo

java中读取各种文件编码的文本文件并正确转码

java读取文本文件经常会出现乱码的情况,我们主要是用下面的两个方法来实现转码:

1:new String("").getBytes("charSetName");

2:new String(bytes,"charSetName");

具体可参考如下代码:

package com.xue.tools;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.Arrays;public class Test1 {public static void main(String[] args) throws IOException {File file = new File("htmlTemplete/Index.html");FileInputStream fileInputStream=new FileInputStream(file);byte[] b=new byte[1024];byte[] B=new byte[0];int read =-1; while ((read=fileInputStream.read(b))>-1) {int i=B.length;B=Arrays.copyOf(B, B.length+read);for(int j=0;j<read;j++){B[i+j]=b[j];}}System.out.println(new String(B,"UTF-16"));}}

?

读书人网 >编程

热点推荐