首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Eclipse开发 >

读取解决eclipse工程中中文乱码的一种提案

2013-01-05 
读取解决eclipse工程中中文乱码的一种方案昨天因为svn更新时出错,导致了所有的包含中文的java文件出现乱码

读取解决eclipse工程中中文乱码的一种方案
昨天因为svn更新时出错,导致了所有的包含中文的java文件出现乱码,用记事本读取中文显示正常,但用eclipse就显示错误,导致问题出现的原因是编码问题,于是写了一个java类,遍历文件夹里所有的java类,并且将其转换成utf-8编码。具体代码如下:

/** * 处理学习元乱码问题 *  * @author 丁国柱  2012-11-22 */public class ParseFile2UFT8 {private static ArrayList<String> filelist = new ArrayList<String>();public static void main(String[] args) throws Exception {String filePath = "F:/program/Lcell/src";// getFiles(filePath);String fileName = "";List<String> fileList = getFiles(filePath);System.out.println("一共有" + fileList.size() + "个文件");for (int i = 0; i < fileList.size(); i++) {// convertFiles(fileList.get(i),);// System.out.println(fileList.get(i));// convertFiles(fileList.get(i).toString(),changeFilePath(fileList.get(i).toString()));fileName = fileList.get(i).toString();System.out.println("处理第" + i + 1 + "个文件:" + fileName);convertFiles(fileName, changeFilePath(fileName));}// System.out.println(changeFilePath("F:/program/Lcell/src/TestHellp.java"));}/* * 通过递归得到某一路径下所有的目录及其文件 */static List<String> getFiles(String filePath) {File root = new File(filePath);File[] files = root.listFiles();for (File file : files) {if (file.isDirectory()) {/* * 递归调用 */getFiles(file.getAbsolutePath());} else {// 只要后缀名为java的文件if (file.getAbsolutePath().substring(file.getAbsolutePath().length() - 5,file.getAbsolutePath().length()).equals(".java")) {filelist.add(file.getAbsolutePath());}}}return filelist;}public static void convertFiles(String inputFilePath, String outputFilePath) {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream(new File(inputFilePath));fos = new FileOutputStream(new File(outputFilePath));byte[] buffer = new byte[fis.available()];fis.read(buffer);String str = new String(buffer, "GBK");fos.write(str.getBytes("utf-8"));} catch (Exception e) {e.printStackTrace();} finally {if (fis != null) {try {fis.close();} catch (Exception e) {}}if (fos != null) {try {fos.close();} catch (Exception e) {}}}}/** * 改变存储路径F:/program/Lcell/src/TestHellp.java * ==>F:/program/Lcell/convert/TestHellp.java *  * @param filePath * @return */public static String changeFilePath(String filePath) {int index = filePath.lastIndexOf("\\src");String newFilePath = filePath.substring(0, index) + "_convert"+ filePath.substring(index, filePath.length());// if(File(newFilePath))File file = new File(newFilePath);if (!file.getParentFile().exists()) {System.out.println("目标文件所在路径不存在,准备创建:" + file.getParentFile()+ "目录");if (!file.getParentFile().mkdirs()) {System.out.println("创建目录文件所在的目录失败!");}}return newFilePath;}// 根据文件名判断是否utf-8编码public static boolean fileIsUTF8Encoding(FileInputStream in) {boolean isUtf8 = false;try {byte[] b = new byte[3];try {in.read(b);in.close();if (b[0] == -17 && b[1] == -69 && b[2] == -65) {// 是utf-8编码isUtf8 = true;}} catch (FileNotFoundException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}return isUtf8;}}

热点排行