看到一篇关于eclipse导入项目,java文件中文乱码的解决方案,先mark下

注:本文来自eclipse导入项目,java文件中文乱码的解决方案

/**
 * 建议在转换前先将代码备份
 * 
 * @date 2012-5-23
 */

public class ConverEncoding {

	public static void main(String[] args) throws Exception {
		String srcDir = "F:\\androidproject\\weibo.sdk.android.demo";
		List<String> files = new ArrayList<String>();
		fetchFileList(srcDir, files, ".java");
		for (String fileName : files) {
			// /
			convert(fileName, "UTF-8", fileName, "GBK");
		}
	}

	/**
	 * 将旧文件的编码更改成另一种编码 并保存到新文件中
	 * 
	 * @param oldFile
	 *            旧文件
	 * @param oldCharset
	 *            旧文件的编码
	 * @param newFlie
	 *            新文件
	 * @param newCharset
	 *            新文件的编码
	 */

	public static void convert(String oldFile, String oldCharset,
			String newFlie, String newCharset) {
		BufferedReader bin;
		FileOutputStream fos;
		StringBuffer content = new StringBuffer();
		try {
			System.out.println(oldFile);
			// 
			bin = new BufferedReader(new InputStreamReader(new FileInputStream(
					oldFile), "UTF-8"));
			String line = null;
			while ((line = bin.readLine()) != null) {
				// System.out.println("content:" + content);
				content.append(line);
				content.append(System.getProperty("line.separator"));
			}
			bin.close();
			File dir = new File(newFlie.substring(0, newFlie.lastIndexOf("\\")));
			if (!dir.exists()) {
				dir.mkdirs();
			}
			fos = new FileOutputStream(newFlie);
			Writer out = new OutputStreamWriter(fos, newCharset);
			out.write(content.toString());
			out.close();
			fos.close();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void fetchFileList(String strPath, List<String> filelist,
			final String regex) {
		File dir = new File(strPath);
		File[] files = dir.listFiles();
		Pattern p = Pattern.compile(regex);
		if (files == null)
			return;
		for (int i = 0; i < files.length; i++) {
			if (files[i].isDirectory()) {
				fetchFileList(files[i].getAbsolutePath(), filelist, regex);
			} else {
				String strFileName = files[i].getAbsolutePath().toLowerCase();
				Matcher m = p.matcher(strFileName);
				if (m.find()) {
					filelist.add(strFileName);
				}
			}
		}
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值