批量文件编码转换(GBK/UTF-8/UNICODE etc)

来源:互联网 发布:安卓人脸识别源码 编辑:程序博客网 时间:2024/04/26 15:22

批量文件编码转换,JAVA源码:代码中的GBK为文件编码格式,Unicode为目标文件编码格式,根据个人实际情况切换编码。

warn:因为文件是一次性读取全部字符,所以单个文件大小不要太大,否则会内存溢出的。

做好文件备份。

因为eclipse或者其它IDE里的代码只能编辑当前工程目录下的文件夹或者文件,对于不属于workspace的File是没有权限写入的,we‘d better 把需要转码的文件夹放在工程目录下进行转换。’


import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.nio.charset.Charset;public class C {public static void main(String[] args) throws IOException {// TODO Auto-generated method stubFile file=new File("E:\\workspace\\AA\\src\\src");try {transfer2Unicode(file);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}//E:\mobileBeta\com.agree.abt.mob\platforms\android\src\cn\com\agree\amate//File file=new File("E:\\workspace\\AA\\src\\src\\android\\src\\cn\\com\\agree\\amate\\AMate.java");//InputStreamReader isr=new InputStreamReader(new FileInputStream(file), Charset.forName("unicode"));//char[] chars=new char[1];//while(isr.read(chars)!=-1){//System.out.print(chars[0]);//}//String unicode="阿萨德发生";//byte[] utf=unicode.getBytes(Charset.forName("UTF-8"));//System.out.println(new String(utf,Charset.forName("UTF-8")));//System.out.println(new String(unicode.getBytes(Charset.forName("unicode")),Charset.forName("unicode")));//File file=new File("E:\\workspace\\AA\\src\\src/unicode");//OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(file), Charset.forName("unicode"));//osw.write(new String(unicode.getBytes(Charset.forName("unicode")),Charset.forName("unicode")).toCharArray());//osw.flush();//osw.close();//File[] files=file.listFiles();//for(File everyFile:files){//if(everyFile.isFile()){//InputStreamReader isr=new InputStreamReader(new FileInputStream(everyFile), Charset.forName("GBK"));//StringBuffer sb=new StringBuffer();//char[] chars=new char[1];//while(isr.read(chars)!=-1){//sb.append(chars);//}//String str=new String(sb.toString().getBytes(Charset.forName("GBK")));//String unicode=new String(str.getBytes(Charset.forName("unicode")),Charset.forName("unicode"));//System.out.print(unicode);////}//}}public static void transfer2Unicode(File file) throws IOException{if(file.exists()){if(file.isDirectory()){File[] files=file.listFiles();for(File unit:files){transfer2Unicode(unit);}}else{InputStreamReader isr=new InputStreamReader(new FileInputStream(file), Charset.forName("GBK"));StringBuffer sb=new StringBuffer("");char[] chars=new char[1];while(isr.read(chars)!=-1){sb.append(chars);}String str=new String(sb.toString().getBytes(Charset.forName("GBK")));String unicode=new String(str.getBytes(Charset.forName("unicode")),Charset.forName("unicode"));//File unicodeFile=new File(file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf("\\")-1)+"/"+file.getName());OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(file),Charset.forName("unicode"));System.out.println(unicode.toString());osw.write(unicode.toCharArray());osw.flush();osw.close();isr.close();}} }}

0 0
原创粉丝点击