首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

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

2012-09-01 
java中读取各种文件编码的文本文件并正确转码java读取文本文件经常会出现乱码的情况,我们主要是用下面的两

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"));}}

?

热点排行