首页 > 代码库 > java将文件转为UTF8工具类

java将文件转为UTF8工具类

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工具类