当前位置: 代码迷 >> Eclipse >> 疯子解决eclipse 编码有关问题的小工具
  详细解决方案

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

热度:69   发布时间:2016-04-23 01:17:56.0
疯子解决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) {				}			}		}	}}
1 楼 loloku 18 小时前  
对你的贡献表示支持,但是同时我有个疑问,为什么不在一开始的时候就把GBK调成UTF-8?
2 楼 zh_harry 18 小时前  
这个工具是为编码不统一的情况开发的,如果编码统一的话就不用转码了,一开始调成UTF-8是标准的流程,但有些老项目或其他公司的项目会遇到这种情况,还有有些人没有在一开始配成UTF-8,以后再改真的很麻烦,这也是eclipse的不足吧,它应该支持!
  相关解决方案