最近导了一份项目,是gbk的,但我工作空间里其他项目都是utf-8,改来改去十分麻烦,网上找了个改编码的小java程序,小改了一下,只转换java文件. 需要commons-io.jar ,附件里已有,项目自行加上即可.
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; public class FileUtils extends org.apache.commons.io.FileUtils { private static final String ENCODE_GBK = "GBK"; private static final String ENCODE_UTF8 = "UTF-8"; public static void convertGBK2UTF8(File file) { convertFileEncode(file, ENCODE_GBK, ENCODE_UTF8); } public static void convertUTF82GBK(File file) { convertFileEncode(file, ENCODE_UTF8, ENCODE_GBK); } public static void convertFileEncode(File file, String fromEncode,String toEncode) { try { String str = readFileToString(file, fromEncode); writeStringToFile(file, str, toEncode); } catch (IOException e) { System.out.println("Convert failed. File not exsit?"); e.printStackTrace(); } } public static String readFile(String filePath, String encode) { String fileContent = ""; try { FileInputStream fis = new FileInputStream(filePath); InputStreamReader isr = new InputStreamReader(fis, encode); BufferedReader br = new BufferedReader(isr); StringBuffer sb = new StringBuffer(); for (String line = br.readLine(); line != null; line = br .readLine()) { sb.append(line); } fileContent = sb.toString(); br.close(); isr.close(); fis.close(); } catch (FileNotFoundException e) { System.out.println("File not exist:" + filePath); } catch (IOException e) { e.printStackTrace(); } return fileContent; } public static String saveFile(String fileContent, String encode, String savePath) { try { FileOutputStream fos = new FileOutputStream(savePath); OutputStreamWriter osw = new OutputStreamWriter(fos, encode); BufferedWriter bw = new BufferedWriter(osw); bw.write(fileContent); bw.close(); osw.close(); fos.close(); } catch (FileNotFoundException e) { System.out.println("File not exist:" + savePath); } catch (IOException e) { e.printStackTrace(); } return fileContent; } public static synchronized void convertDirectory(File dir) throws IOException { if (!dir.exists() && !dir.isDirectory()) { throw new IOException("[" + dir + "] not exsit or not a Directory"); } convert(dir); } public static void convert(File dir) { if (dir.canRead() && dir.canWrite()) { if (dir.isDirectory()) {// Directory String[] files = dir.list(); if (files != null) { for (int i = 0; i < files.length; i++) { convert(new File(dir, files[i]));// 递归 } } } else {// File if(dir.getName().endsWith(".java")){ System.out.println(dir.getName()); //需要配置一下转换格式 谁转谁 convertGBK2UTF8(dir); //gbk转为utf-8 //convertUTF82GBK(dir);// utf-8 转为gbk } } } } public static void main(String[] args) throws IOException { long t1 = System.currentTimeMillis(); File src = new File("D:\\Eclipse_studio_4.3.1\\workspace\\kq"); // 注意98行 需要选择一下 convertDirectory(src); long t2 = System.currentTimeMillis(); System.out.println("转换完成,用时:" + (t2 - t1) / 1000); } }
相关推荐
GBK、UTF-8批量文件3秒快速转码工具(支持GBK,UTF-8免费转换),UTF-8/GBK编码在线转换工具,压缩包可以有多目录与文件,如目录中有图片不会转码,但是会随转码好的文件一起打包下载。 使用帮助 . 上传压缩包(仅支持zip...
批量文件转码工具(支持 GBK,UTF-8 转换)。 批量文件转码工具,支持GBK,UTF-8转换,window下面使用,亲测可以使用!!!!!!!!!! 批量 文件转码 工具 GBK UTF-8
老项目采用GBK编码格式,而新项目采用的UTF-8编码格式,如果直接把Java源代码复制到Eclipse中所有的中文信息会出现乱码。所以写了个小的方法类,将java文件的编码格式从GBK转UTF-8
批量文件转码工具,支持GBK,UTF-8转换,window下面使用,亲测可以使用!!!!!!!!!!
转码工具 解决项目中乱码问题 完成gbk到utf-8的双向转换
Red UTF-8 将GBK代码快速批量转换为UTF-8的工具使用本软件可一次性将整站默认代码(GBK及所有默认代码)转换为UTF-8 目前有很多网友需要UTF-8的程序,很多网友想把GBK代码或默认的任何代码想转为UTF-8大多都是手工...
批量文件转码工具,支持GBK,UTF-8转换,window下面使用,亲测可以使用!
GBK转UTF-8工具,支持java工程批量转换
自制软件,文本批量格式转换,将ansi(即gbk)与utf-8相互转换,自己编写用来在浏览器上读txt小说时,避免乱码用的,共享出来方便大家使用,支持gbk和utf-8。
全功能转换GBK,BIG5,UTF-8软件,方便各位转换ASP,PHP源码。 很多朋友都在问要utf或者big5的插件,在此送上本人网上找到的一款免费软件,优点是可以快速的转换文档编码,而且一次性输入目录之后会自动查找目录下要转换的...
编码(UTF-8 GBK等等)批量转换 支持各种格式 非常适用大量需要转换编码的文件
在开发的时候经常碰到这样的情况,需要将原本编码格式为GBK的工程改成UTF-8来编码,设置之后,注释全都乱码了,一个一个改太麻烦,...ps:如果是UTF-8转GBK,或是其他编码格式转换都是可以的,改改源码里面的设置就OK了
注意,GBK的模板转换为utf-8的时候,一些标点是无法转换的,而这些GBK编码的标点在老外的电脑上是无法显示的。所以有些文件里面需要批量替换一些不能转换的东西。同样,中文网站使用的uft-8模板不是纯utf-8,想翻译...
很好用的 GBK/BIG/UTF-8 文件批量互相编码转换工具
GBK批量转utf8(支持整个目录),转换后,将原来的问题增加bak进行备份,也可以取消该选项。
GBK-GB2312-UTF-8编码批量转换
GBK GB2312转UTF-8工具.可以批量地将文件编码进行转换.
软件支持UTF-8,GB,BIG5文件类型之间的转换. 大家可以试试,很不错的东东.呵呵~~