疯子解决eclipse 编码问题的小工具

eclipse 默认为中文编码为gbk,当用gbk环境写的代码,其物理文件的编码为gbk.即使用eclipse 改为utf-8其物理文件的编码仍不变,所以通过以下代码生成。



package com.sparrow.utils;

import java.io.BufferedReader;
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 com.sparrow.constant.COMMON;
import com.sparrow.core.Log;

public class ConvertEncoding {
private static boolean isTest = true;
private static String sourceEncoding = "gb2312";
private static String descEncoding = "UTF-8";

/**
* @author zlz
*
* @time 2013-7-16上午10:26:40
* @param args
*/
public static void main(String[] args) {

BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String rootPath = null;
String yn = null;
System.out.println("请输入要转换的路径");
try {
rootPath = bf.readLine();
} catch (IOException e) {
}

Convert(rootPath);
System.out.println("请输入文件的当前编码,回车默认为gbk");
try {
String tempEncoding = bf.readLine();
if (!"".equals(tempEncoding)) {
sourceEncoding = bf.readLine();
}
} catch (IOException e) {
}

System.out.println("请输入目标文件编码,回车默认为utf-8");
try {
String tempEncoding = bf.readLine();
if (!"".equals(tempEncoding)) {
descEncoding = bf.readLine();
}
} catch (IOException e) {
}

System.out.println("立即生成吗?Y/N");

try {
yn = bf.readLine();
} catch (IOException e) {
}

if (yn.toLowerCase().equals("y")) {
isTest = false;
Convert(rootPath);
System.out.println("ending");
}
}

public static void Convert(String path) {
File file = new File(path);
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory() && !f.isHidden()) {
Convert(f.getPath());
} else {
String fileName = f.getPath();
String sourceText = readFileContent(fileName, sourceEncoding);
if (fileName.endsWith(".java")) {
if (isTest) {
System.out.println(sourceText);
} else {
writeFile(fileName, sourceText, descEncoding);
System.out.println(fileName + " is encoded");
}
}
}
}
}

/**
* 以行为单位读取文件,常用于读面向行的格式化文件
*/
public static String readFileContent(String fileName, String charset) {
if (StringUtil.isNullOrEmpty(charset)) {
charset = "UTF-8";
}
File file = new File(fileName);
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(file), charset));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
sb.append(tempString);
sb.append(COMMON.ENTER_TEXT);
}
reader.close();
} catch (IOException e) {
Log.getInstance().error(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return sb.toString();
}

public static boolean writeFile(String filePath, String s, String charset) {
OutputStreamWriter osw = null;
try {
osw = new OutputStreamWriter(new FileOutputStream(filePath),
charset);
osw.write(s, 0, s.length());
osw.flush();
return true;
} catch (Exception e) {
Log.getInstance().error(e);
return false;
} finally {
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
}
}
}
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值