目录文件编码转换

本文介绍了一个用于改变整个目录编码的类,帮助解决在使用Eclipse导入工程时遇到的中文乱码问题。通过手动设置源文件和输出文件的编码,程序能够自动处理目录下的所有文件,确保正确显示中文字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候导入一个工程到Eclipse中发现中文乱码了。原因就是工程的编码格式变了。比如从UTF-8变成了GBK等等。此时我们手动去改的话十分麻烦。下面这个类就是用来改变整个目录的编码的,详细请看代码。

    import java.io.BufferedReader;     
    import java.io.BufferedWriter;     
    import java.io.File;     
    import java.io.FileFilter;     
    import java.io.FileInputStream;     
    import java.io.FileOutputStream;     
    import java.io.IOException;     
    import java.io.InputStream;     
    import java.io.InputStreamReader;     
    import java.io.OutputStream;     
    import java.io.OutputStreamWriter;     
    import java.io.Reader;     
    import java.io.UnsupportedEncodingException;     
    import java.io.Writer;     
        
    public class FileEncodeConverter {     
        
        // 原文件目录     
        private static String srcDir = "x:/src";     
        // 转换后的存放目录     
        private static String desDir = "x:/des";      
        // 源文件编码     
        private static String srcEncode = "gb2312";     
        // 输出文件编码     
        private static String desEncode = "utf-8";     
             
        // 处理的文件过滤     
        private static FileFilter filter = new FileFilter() {     
            public boolean accept(File pathname) {     
                // 只处理:目录 或是 .java文件     
                if (pathname.isDirectory()     
                        || (pathname.isFile() && pathname.getName().endsWith(     
                                ".java")))     
                    return true;     
                else    
                    return false;     
            }     
        };     
             
        /**   
         * @param file   
         */    
        public static void readDir(File file)     
        {     
            File[] files = file.listFiles(filter);     
            for (File subFile : files) {     
                // 建立目标目录     
                if (subFile.isDirectory()) {     
                    File file3 = new File(desDir + subFile.getAbsolutePath().substring(srcDir.length()));     
                    if (!file3.exists()) {     
                        file3.mkdir();     
                    }     
                    file3 = null;     
                    readDir(subFile);     
                } else {     
                    System.err.println("一源文件:\t"+subFile.getAbsolutePath() + "\n目标文件:\t" + (desDir + subFile.getAbsolutePath().substring(srcDir.length())));     
                    System.err.println("-----------------------------------------------------------------");     
                    try {     
                        convert(subFile.getAbsolutePath(), desDir + subFile.getAbsolutePath().substring(srcDir.length()), srcEncode, desEncode);     
                    } catch (UnsupportedEncodingException e) {     
                        e.printStackTrace();     
                    } catch (IOException e) {     
                        e.printStackTrace();     
                    }     
                }     
            }     
        }     
             
        /**   
         *    
         * @param infile    源文件路径   
         * @param outfile   输出文件路径   
         * @param from  源文件编码   
         * @param to    目标文件编码   
         * @throws IOException   
         * @throws UnsupportedEncodingException   
         */    
        public static void convert(String infile, String outfile, String from,     
                String to) throws IOException, UnsupportedEncodingException {     
            // set up byte streams     
            InputStream in;     
            if (infile != null)     
                in = new FileInputStream(infile);     
            else    
                in = System.in;     
            OutputStream out;     
            if (outfile != null)     
                out = new FileOutputStream(outfile);     
            else    
                out = System.out;     
        
            // Use default encoding if no encoding is specified.     
            if (from == null)     
                from = System.getProperty("file.encoding");     
            if (to == null)     
                to = System.getProperty("file.encoding");     
        
            // Set up character stream     
            Reader r = new BufferedReader(new InputStreamReader(in, from));     
            Writer w = new BufferedWriter(new OutputStreamWriter(out, to));     
        
            // Copy characters from input to output. The InputStreamReader     
            // converts from the input encoding to Unicode,, and the     
            // OutputStreamWriter     
            // converts from Unicode to the output encoding. Characters that cannot     
            // be     
            // represented in the output encoding are output as '?'     
            char[] buffer = new char[4096];     
            int len;     
            while ((len = r.read(buffer)) != -1)     
                w.write(buffer, 0, len);     
            r.close();     
            w.flush();     
            w.close();     
        }     
             
        
        public static void main(String[] args) {     
            // 建立目标文件夹     
            File desFile = new File(desDir);     
            if (!desFile.exists()) {     
                desFile.mkdir();     
            }     
            desFile = null;     
        
            File srcFile = new File(srcDir);     
            // 读取目录 循环转换文件     
            readDir(srcFile);     
            srcFile = null;     
        }     
        
    }     


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值