Eclipse编码问题

通常在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

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。