目录文件编码转换

有时候导入一个工程到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;     
        }     
        
    }     


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: UTF-8文件编码转换工具是一种可以将文件从一种编码格式转换为UTF-8格式的工具。由于不同的国家和地区使用不同的字符编码系统,当我们需要在不同的平台或程序之间共享文件,可能会遇到编码转换的问题。UTF-8是一种通用的字符编码标准,它可以表示广泛的字符集,包括大多数世界上的语言。 UTF-8文件编码转换工具可以通过简单易用的界面,将文件从其他编码格式(如GB2312、ISO-8859-1等)转换为UTF-8格式。用户只需选择要转换文件和目标编码格式,点击转换按钮即可完成转换过程。转换完成后,用户可以在所选的目标目录中找到转换后的UTF-8编码文件。 UTF-8文件编码转换工具的作用主要有两个方面。首先,它可以帮助用户解决在不同编码环境下文件编码不兼容的问题,确保文件的正确显示和处理。其次,它可以提高文件的可移植性,使其可以在不同的平台和程序中使用和共享。 需要注意的是,使用UTF-8文件编码转换工具应谨慎检查转换结果,以确保转换过程没有引入任何错误或变化。同,用户也应了解不同编码格式之间的差异,避免意外丢失或损坏文件中的特定字符或信息。 总之,UTF-8文件编码转换工具是一种方便实用的工具,可以帮助用户解决不同编码格式之间的兼容性问题,并提高文件的可移植性和共享性。 ### 回答2: UTF-8文件编码转换工具是一种用于将文件从一种编码形式转换成另一种编码形式的工具。UTF-8文件编码是一种用于在计算机中存储和传输Unicode字符的编码方式,它为每个字符分配了不同的字节数,从而可以表示包括中文在内的几乎所有字符。 UTF-8文件编码转换工具通常具有以下功能: 1. 编码转换:可以将一个编码格式的文件转换成UTF-8编码格式。这在需要将其他编码格式的文件转换为普遍支持的UTF-8编码格式非常有用。 2. 解码转换:可以将UTF-8编码格式的文件转换成其他编码格式。这在需要将UTF-8编码格式的文件转换为其他特定编码格式很有用。 3. 批量转换:可以同处理多个文件编码转换操作,提高转换效率。 4. 提供预览和比较功能:可以预览转换前后文件的内容,并提供比较功能以确保转换的正确性。 5. 异常处理:可以处理转换过程中可能出现的异常情况,如文件格式错误或不支持的编码格式。 使用UTF-8文件编码转换工具可以轻松地在不同的编码格式之间进行转换,解决文件编码不兼容的问题,确保文件内容的正确性和一致性。这对于在多国语言环境下进行文件处理和共享非常重要,尤其是在涉及中文等非ASCII字符的情况下。 ### 回答3: UTF-8文件编码转换工具是一种能够将不同编码格式的文件转换为UTF-8编码的软件或工具。UTF-8是一种常用的多字节字符集编码,可以表示全世界几乎所有的字符。在国际化和跨平台应用中,使用UTF-8编码可以避免字符编码不一致带来的问题。 UTF-8文件编码转换工具的功能主要包括:检测文件编码、将其他编码文件转换为UTF-8编码、批量转换多个文件、保存文件指定UTF-8编码等。 使用UTF-8文件编码转换工具有以下几个步骤: 首先,我们需要确认源文件编码格式。可以使用工具提供的检测功能,通过分析文件的字节序列来推测文件编码格式。 其次,选择正确的目标编码格式为UTF-8。如果源文件编码与目标编码不一致,我们需要使用工具提供的转换功能将文件编码格式转换为UTF-8。 然后,我们可以选择批量转换多个文件,方便处理大量文件编码转换需求。 最后,保存文件需要指定UTF-8编码,确保文件以UTF-8编码格式保存。 需要注意的是,UTF-8文件编码转换工具并不会对文件内容进行修改,只是修改文件编码格式。因此,在转换文件编码前,建议先备份原始文件,以免不可挽回地修改文件内容。 总结来说,UTF-8文件编码转换工具是一种帮助用户将其他编码格式的文件转换为UTF-8编码的实用工具,可以避免在不同平台或国际化应用中出现的字符编码不一致问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值