• 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

  • 相关阅读:
    Java线上应用故障排查之一:高CPU占用【转】
    JAVA 之 Tomcat知识框架【转】
    FileZilla 配置备份与还原【转】
    linux删除第几天日志【原创】
    sar命令使用【转】
    Mysql Group Replication 简介及单主模式组复制配置【转】
    grep 中的正则表达式【转】
    java程序out of memory【转】
    Java笔记18:JUnit单元测试
    Java笔记17:导出可执行jar包
  • 原文地址:https://www.cnblogs.com/pandans/p/4162706.html
Copyright © 2020-2023  润新知