Eclipse編碼問題

來源:互聯網
上載者:User

標籤:des   blog   io   ar   os   sp   for   java   on   

通常在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 stubtry {exchange(SOURCE_DIR);} catch (Exception e) {// TODO Auto-generated catch blockXje.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 blocke.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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.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編碼問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.