码迷,mamicode.com
首页 > 编程语言 > 详细

java将文件转为UTF8工具类

时间:2014-09-19 17:08:45      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   color   io   os   java   ar   文件   

package hiveTest;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class GBKTransferUTF8 {
    
    
    private static void transferFile(String srcFileName, String destFileName) throws IOException {
          String line_separator = System.getProperty("line.separator"); 
          FileInputStream fis = new FileInputStream(srcFileName);
          StringBuffer content = new StringBuffer();
          DataInputStream in = new DataInputStream(fis);
          BufferedReader d = new BufferedReader(new InputStreamReader(in, "GBK"));// , "UTF-8"  
          String line = null;
          while ((line = d.readLine()) != null)
           content.append(line + line_separator);
          d.close();
          in.close();
          fis.close();
              
          Writer ow = new OutputStreamWriter(new FileOutputStream(destFileName), "utf-8");
          ow.write(content.toString());
          ow.close();
         }
    
    
        public static void main(String[] args) throws IOException {
            
            String srcFileName="C:\\Users\\lenovo\\Desktop\\大数据设计\\community_data\\data_example_to_qiaokai.txt";
            String destFileName="C:\\Users\\lenovo\\Desktop\\大数据设计\\community_data\\data_example_to_qiaokai-utf8.txt";
            GBKTransferUTF8.transferFile(srcFileName, destFileName);
        }
}

 

java将文件转为UTF8工具类

标签:des   style   blog   color   io   os   java   ar   文件   

原文地址:http://www.cnblogs.com/joqk/p/3981490.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!