1、将本地的文件转换成另外一种编码输出,主要逻辑代码如下:
1 /** 2 * 将本地文件以哪种编码输出 3 * @param inputfile 输入文件的路径 4 * @param outfile 输出文件的路径 5 * @param code 输出文件的编码 6 * @throws IOException 7 */ 8 public void convert(String inputfile,String outfile,String code) throws IOException { 9 StringBuffer sb = new StringBuffer(); 10 //得到当前文件的编码 11 String ch=getCharset(inputfile); 12 InputStreamReader isr=null; 13 OutputStreamWriter osw =null; 14 //根据当前文件编码进行解码 15 if(ch.equals("UTF8")){ 16 isr= new InputStreamReader(new FileInputStream(inputfile), "UTF-8"); 17 }else if(ch.equals("Unicode")){ 18 isr= new InputStreamReader(new FileInputStream(inputfile), "Unicode"); 19 }else { 20 isr= new InputStreamReader(new FileInputStream(inputfile), "GB2312"); 21 } 22 //将字符串存入StringBuffer中 23 BufferedReader br = new BufferedReader(isr); 24 String line = null; 25 while ((line = br.readLine()) != null) { 26 sb.append(line + "\n"); 27 } 28 br.close(); 29 isr.close(); 30 31 //以哪种方式写入文件 32 if("UTF-8".equals(code)){ 33 osw = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8"); 34 }else if("GB2312".equals(code)){ 35 osw = new OutputStreamWriter(new FileOutputStream(outfile), "GB2312"); 36 }else if("Unicode".equals(code)){ 37 osw = new OutputStreamWriter(new FileOutputStream(outfile), "Unicode"); 38 }else{ 39 osw = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8"); 40 } 41 BufferedWriter bw = new BufferedWriter(osw); 42 bw.write(sb.toString()); 43 bw.close(); 44 osw.close(); 45 } 46 47 /** 48 * 根据文件路径判断编码 49 * @param str 50 * @return 51 * @throws IOException 52 */ 53 private String getCharset(String str) throws IOException{ 54 BytesEncodingDetect s = new BytesEncodingDetect(); 55 String code = BytesEncodingDetect.javaname[s.detectEncoding(new File(str))]; 56 return code; 57 }
2、将远程的文件转换成自己想要的编码,然后写入本地
1 /** 2 * 将远程文件以哪种编码输出到本地 3 * @param fileurl 远程文件的URL 4 * @param outfile 输出文件的路径 5 * @param code 以哪种编码输出 6 * @throws Exception 7 */ 8 public void Remote(String fileurl,String outfile,String code) throws Exception{ 9 // String str="http://172.18.1.103:8080/kms/text.txt"; 10 URL url =new URL(fileurl); 11 HttpURLConnection urlCon = (HttpURLConnection)url.openConnection(); 12 //设置连接超时和读取超时 13 urlCon.setConnectTimeout(5000); 14 urlCon.setReadTimeout(5000); 15 //得到远程文件的编码 16 String ch=getCharset_Remote(fileurl); 17 //文件写入流 18 OutputStreamWriter osw =null; 19 BufferedReader br=null; 20 //将字符串保存到临时的StringBuffer中 21 StringBuffer sb = new StringBuffer(); 22 if(ch.equals("UTF8")){ 23 br =new BufferedReader(new InputStreamReader( urlCon.getInputStream(),"UTF-8")); 24 String line = null; 25 while ((line = br.readLine()) != null) { 26 sb.append(line + "\n"); 27 } 28 } 29 else if(ch.equals("Unicode")){ 30 br =new BufferedReader(new InputStreamReader( urlCon.getInputStream(),"Unicode")); 31 String line = null; 32 while ((line = br.readLine()) != null) { 33 sb.append(line + "\n"); 34 } 35 } 36 else{ 37 br =new BufferedReader(new InputStreamReader( urlCon.getInputStream(),"GB2312")); 38 String line = null; 39 while ((line = br.readLine()) != null) { 40 sb.append(line + "\n"); 41 } 42 } 43 br.close(); 44 45 //以哪种方式写入文件 46 if("UTF-8".equals(code)){ 47 osw = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8"); 48 }else if("GB2312".equals(code)){ 49 osw = new OutputStreamWriter(new FileOutputStream(outfile), "GB2312"); 50 }else if("Unicode".equals(code)){ 51 osw = new OutputStreamWriter(new FileOutputStream(outfile), "Unicode"); 52 }else{ 53 osw = new OutputStreamWriter(new FileOutputStream(outfile), "UTF-8"); 54 } 55 BufferedWriter bw = new BufferedWriter(osw); 56 bw.write(sb.toString()); 57 bw.close(); 58 osw.close(); 59 } 60 61 /** 62 * 根据文件路径判断远程文件编码 63 * @param str 64 * @return 65 * @throws IOException 66 */ 67 private String getCharset_Remote(String str) throws IOException{ 68 URL url =new URL(str); 69 BytesEncodingDetect s = new BytesEncodingDetect(); 70 String fileCode = BytesEncodingDetect.javaname[s.detectEncoding(url)]; 71 return fileCode; 72 }
3、测试代码:
1 package com.zhang.test; 2 3 public class test { 4 public static void main(String[] args) { 5 String inputfile="D:/gbk.txt"; 6 String outfile="D:/utf8.txt"; 7 String outfileurl="D:/utf8url.txt"; 8 String fileurl="http://127.0.0.1:8080/jsp/gbk.txt"; 9 // String file="D:/unicode.txt"; 10 Convert_Code code=new Convert_Code(); 11 try { 12 code.convert(inputfile,outfile,"UTF-8"); 13 code.Remote(fileurl, outfileurl, "UTF-8"); 14 } catch (Exception e) { 15 e.printStackTrace(); 16 } 17 } 18 }
注意:这两个方法引用了BytesEncodingDetect.java文件
此案例的下载链接:http://download.csdn.net/detail/u013865056/9908100