通常在Eclipse下,mac和windows编码是不一样的。如果含有中文java sources通常会出现乱码。

解决---小程序!

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset; public class CharEncodingExchange { private final static String SOURCE_ENCODING = "EUC-CN"; private final static String TARGET_ENCODING = "UTF-8"; private static String SOURCE_DIR = "/Users/pandans/Desktop/CoastalBank/src"; private static String TARGET_DIR = "/Users/pandans/Desktop/tmp"; /**
*
* @param args
*/ public static void main(String[] args) { // TODO Auto-generated method stub try { exchange(SOURCE_DIR); } catch (Exception e) { // TODO Auto-generated catch blockXj e.printStackTrace(); } } /**
*
* exchange the character encoding from srcDir to targetDir
*
*
*
* @param srcDir
*
* @param targetDir
*/ public static void exchange(String srcDir) { String absPath = ""; if (!srcDir.equals(SOURCE_DIR)) { absPath = srcDir.substring(SOURCE_DIR.length()); String targetDir = TARGET_DIR + absPath; File targetDirectory = new File(targetDir); if (targetDirectory.isDirectory() && !targetDirectory.exists()) { targetDirectory.mkdirs(); } } File sourceDirectory = new File(srcDir); if (sourceDirectory.exists()) { if (sourceDirectory.isFile()) { String targetFilePath = TARGET_DIR + absPath; try { fileEncodingExchange(sourceDirectory, targetFilePath); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { File[] childs = sourceDirectory.listFiles(); for (File child : childs) exchange(child.getPath()); } } } private static void fileEncodingExchange(File infile, String targetAbsFilePath) throws IOException { FileInputStream fin = null; FileOutputStream fout = null; FileChannel fcin = null; FileChannel fcout = null; System.out.println(infile + " " + targetAbsFilePath); String tmpTargetPath = targetAbsFilePath.substring(0, targetAbsFilePath .lastIndexOf(File.separator)); File tmpTargetDir = new File(tmpTargetPath); if (!tmpTargetDir.exists()) tmpTargetDir.mkdirs(); try { fin = new FileInputStream(infile); fout = new FileOutputStream(targetAbsFilePath); fcin = fin.getChannel(); fcout = fout.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); while (true) { buffer.clear(); int r = fcin.read(buffer); if (r == -1) { break; } buffer.flip(); String encoding = System.getProperty("file.encoding"); fcout.write(ByteBuffer.wrap(Charset.forName(encoding).decode( buffer).toString().getBytes(TARGET_ENCODING))); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (fin != null) { fin.close(); } if (fcin != null) { fcin.close(); } if (fout != null) fout.close(); if (fcout != null) fcout.close(); } } }

自己修改路径。

命令行下运行

编译。

javac CharEncodingExchange.java

运行:

java CharEncodingExchange

先把工程编码变成想要的编码。这里是UTF-8

把tmp的文件覆盖到工程src下。

--------------Eclipse修改默认编码---------------

UTF-8

这个修改了整个工程会自动变。

UTF-8

Eclipse编码问题的更多相关文章

  1. eclipse 编码设置

    eclipse 编码设置 浏览:2840 | 更新:2013-12-31 10:07 一般Java文件编码格式是UTF-8的.以下以默认GBK改为UTF-8为例. 1.改变整个工作空间的编码格式,这样 ...

  2. Eclipse编码规范——Code Templates设置

    Eclipse编码规范——Code Templates设置 Eclipse编码规范主要包括三个方面:设置Code Templates.Eclipse formatter.Checkstyle, 本篇主 ...

  3. 设置Eclipse编码方式

    1.windows->Preferences...打开"首选项"对话框,左侧导航树,导航到 general->Workspace,右侧Text file encodin ...

  4. Eclipse编码设置(转载)

    来源:http://e-ant.javaeye.com/blog/177579 如果要使插件开发应用能有更好的国际化支持,能够最大程度的支持中文输出,则最好使 Java文件使用UTF-8编码.然而,E ...

  5. eclipse color themes 让eclipse编码好看点

    http://eclipsecolorthemes.org/ 就是这个,很好用的! 安装后需要重启Eclipse.重启后打开Window->Preferences->General-> ...

  6. eclipse 编码改成utf-8

    Eclipse的编码格式是系统默认 修改为utf-8,点击Apply and Close 然后项目的编码格式会统一默认utf-8 当然也可以选择other,改成GBK.

  7. eclipse 编码设置【转】

    一般Java文件编码格式是UTF-8的.以下以默认GBK改为UTF-8为例. 1.改变整个工作空间的编码格式,这样以后新建的文件也是新设置的编码格式. eclipse->window->p ...

  8. eclipse编码转换

    一般Java文件编码格式是UTF-8的.以下以默认GBK改为UTF-8为例. 1.改变整个工作空间的编码格式,这样以后新建的文件也是新设置的编码格式.     eclipse->window-& ...

  9. eclipse编码设置

随机推荐

  1. LeetCode 58. Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  2. (转) C# textbox 限制输入问题

    原理:e.handled代表这次按键动作是否交由用户自己处理,如果为true代表由用户处理,系统不再过问,这里的应用是拦截,即通知系统我要处理这个数据,但又不处理(丢弃数据),从而实现拦截的效果. 在 ...

  3. 共享一个MVC通过NPOI导出excel的通用方法

    public static System.IO.MemoryStream ExportExcel<T>(string title, List<T> objList, param ...

  4. vs2013 ie10

    http://blog.163.com/qimo601@126/blog/static/1582209320143354446462/     @ECHO OFF   :IE10HACK  REG A ...

  5. history.back(-1)和history.go(-1)的区别

    history.back(-1):直接返回当前页的上一页,数据全部消息,是个新页面 history.go(-1):也是返回当前页的上一页,不过表单里的数据全部还在 返回到指定连接:document.l ...

  6. 题目1005:Graduate Admission

    题目1005:Graduate Admission 时间限制:1 秒 内存限制:32 兆 特殊判题:否 题目描述: It is said that in 2011, there are about 1 ...

  7. (转)Struts2返回JSON对象的方法总结

    转自:http://kingxss.iteye.com/blog/1622455 如果是作为客户端的HTTP+JSON接口工程,没有JSP等view视图的情况下,使用Jersery框架开发绝对是第一选 ...

  8. 翻译「C++ Rvalue References Explained」C++右值引用详解 Part3:右值引用

    本文为第三部分,目录请参阅概述部分:http://www.cnblogs.com/harrywong/p/4220233.html. 右值引用 如果x是任意类型,那么x&&则被称作一个 ...

  9. iOS copy&mutableCopy理解

    Copy&mutableCopy   通过copy方法可以创建可变或不可变对象的不可变副本,通过mutableCopy可以创建可变或不可变对象的可变副本. 拷贝分为浅拷贝和深拷贝: 浅拷贝:指 ...

  10. js正则表达式中匹配反引号

    直接用反引号就可以了~ /`[\W\w\*]+`\.`[\W\w\*]+`/