本文主要是介绍看到一篇关于eclipse导入项目,java文件中文乱码的解决方案,先mark下,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
转自http://blog.csdn.net/jasonzhou613/article/details/8753628
注:本文来自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);
- }
- }
- }
- }
- }
这篇关于看到一篇关于eclipse导入项目,java文件中文乱码的解决方案,先mark下的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!