eclipseインポートプロジェクトについて、javaファイルの中国語文字化けしの解決策を見て、まずmarkの下で

2410 ワード

注意: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);
				}
			}
		}
	}
}