• 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

  • 相关阅读:
    排序算法合集(冒泡,选择,插入,堆排,快排)
    codeforces 632+ E. Thief in a Shop
    nyoj-一笔画问题-欧拉图+联通判定
    hdu-1179-二分图最大匹配
    hdu-2063-二分图最大匹配
    (转)二分图的最大匹配、完美匹配和匈牙利算法
    hdu-2444-二分图判定+最大分配
    C
    E
    C
  • 原文地址:https://www.cnblogs.com/pandans/p/4162706.html
Copyright © 2020-2023  润新知