批量文件编码转换(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
- 批量文件编码转换(GBK/UTF-8/UNICODE etc)
- linux下GBK->UTF-8文件编码批量转换脚本
- linux下GBK->UTF-8文件编码批量转换脚本
- Linux下GBK->UTF-8文件编码批量转换命令
- linux 下 GBK-UTF-8文件编码批量转换脚本
- Linux下GBK文件编码批量转换UTF-8命令
- UNICODE, GBK, UTF-8编码的转换
- 汉字编码(【Unicode】 【UTF-8】 【Unicode与UTF-8之间的转换】 【汉字 Unicode 编码范围】【中文标点Unicode码】【GBK编码】【批量获取汉字UNICODE码】)
- 多线程批量转换文件编码, 从GBK, GB2312编码转换到UTF-8编码(Python)
- python 之编码转换 unicode, utf-8, utf-16, GBK
- 编码:utf-8,unicode,gbk....
- GBK、UTF-8、Unicode 编码
- Unicode,ISO-8859,GBK,UTF-8编码及相互转换
- c# GBK,Unicode,UTF-8编码与汉字的转换
- 字符串编码格式相互转换Unicode --UTF-8 -- GBK
- 字符编码Unicode UTF-8 GBK的不同和转换
- java 批量文件编码转换 GBK to UTF-8(swing界面)
- 【转载】linux下GBK->UTF- 8文件编码批量转换脚本
- 黄聪:CodeSmith和PowerDesigner的使用安装和数据库创建(原创系列教程)
- Maven Web项目的创建和使用
- 苹果树
- TextView等类似控件的富文本展示
- 微信APP支付,后台回调
- 批量文件编码转换(GBK/UTF-8/UNICODE etc)
- 自动创建UI测试文件
- Spring MVC 4.2 增加 CORS 支持
- linux 添加用户、权限
- excel表格文件如何转换成PDF格式
- 百度百科——BSS段
- opencv中findContours导致crash
- guava笔记三: Concurrency 简单介绍
- Android TypedArray的用法