`
zh_harry
  • 浏览: 99015 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
877aca81-daac-33c8-8bf9-3a886cebc6c3
自己动手写java 框架
浏览量:27067
社区版块
存档分类
最新评论

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

    博客分类:
  • JAVA
阅读更多
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) {
				}
			}
		}
	}

}

1
1
分享到:
评论
4 楼 zh_harry 2013-07-29  
loloku 写道
zh_harry 写道
这个工具是为编码不统一的情况开发的,如果编码统一的话就不用转码了,一开始调成UTF-8是标准的流程,但有些老项目或其他公司的项目会遇到这种情况,还有有些人没有在一开始配成UTF-8,以后再改真的很麻烦,这也是eclipse的不足吧,它应该支持!

兄弟,你说的是对的,按照你的想法,这个可以解决一些编码问题,很高兴能看到这样的文章

这个问题确实很头疼,真正遇到过的才懂的。
3 楼 loloku 2013-07-29  
zh_harry 写道
这个工具是为编码不统一的情况开发的,如果编码统一的话就不用转码了,一开始调成UTF-8是标准的流程,但有些老项目或其他公司的项目会遇到这种情况,还有有些人没有在一开始配成UTF-8,以后再改真的很麻烦,这也是eclipse的不足吧,它应该支持!

兄弟,你说的是对的,按照你的想法,这个可以解决一些编码问题,很高兴能看到这样的文章
2 楼 zh_harry 2013-07-21  
这个工具是为编码不统一的情况开发的,如果编码统一的话就不用转码了,一开始调成UTF-8是标准的流程,但有些老项目或其他公司的项目会遇到这种情况,还有有些人没有在一开始配成UTF-8,以后再改真的很麻烦,这也是eclipse的不足吧,它应该支持!
1 楼 loloku 2013-07-21  
对你的贡献表示支持,但是同时我有个疑问,为什么不在一开始的时候就把GBK调成UTF-8?

相关推荐

Global site tag (gtag.js) - Google Analytics