`
lee79
  • 浏览: 106677 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

File Encoding Converter

阅读更多

在Java应用开发中,经常会遇到不同的开发人员的IDE设置的文件编码格式不一样,在后续集成环境中往往会因为file encoding不同引发一些小问题,故编写了一个小程序做文件转换动作。

 

import java.io.*;

public class FileEncodingConverter {
    //Java file extention
    public static final String FILE_EXTENTION_JAVA = ".java";

    public static final int BUFFER_SIZE = 1024 * 4;

    //File encoding
    public static final String FILE_ENCODING_UTF8 = "UTF_8";
    public static final String FILE_ENCODING_GBK = "GBK";
    public static final String FILE_ENCODING_GB2312 = "gb2312";

    /**
     * File filter, only directory and java file
     */
    private static FileFilter fileFilter = new FileFilter() {
        public boolean accept(File file) {
            //directory and java file
            return file.isDirectory() || (file.isFile() && file.getName().endsWith(FILE_EXTENTION_JAVA));
        }
    };

    /**
     * @param sourceDirectoryPath sourceDirectory
     * @param destDirectoryPath   destDirectory
     * @param sourceFileEncoding  sourceEncoding
     * @param destFileEncoding    destEncoding
     */
    public static void scanDirectory(String sourceDirectoryPath, String destDirectoryPath,
                                     String sourceFileEncoding, String destFileEncoding) {
        File destDirectory = new File(destDirectoryPath);
        if (!destDirectory.exists()) {
            destDirectory.mkdir();
        }

        File sourceDirectory = new File(sourceDirectoryPath);

        scanDirectory(sourceDirectory, sourceDirectoryPath, destDirectoryPath, sourceFileEncoding, destFileEncoding);
    }

    /**
     * @param directory           directory
     * @param sourceDirectoryPath sourceDirectory
     * @param destDirectoryPath   destDirectory
     * @param sourceFileEncoding  sourceEncoding
     * @param destFileEncoding    destEncoding
     */
    private static void scanDirectory(File directory, String sourceDirectoryPath, String destDirectoryPath,
                                      String sourceFileEncoding, String destFileEncoding) {
        File[] files = directory.listFiles(fileFilter);

        File destFile;
        for (File file : files) {
            if (file.isDirectory()) {
                destFile = new File(destDirectoryPath + file.getAbsolutePath().substring(sourceDirectoryPath.length()));
                if (!destFile.exists()) {
                    destFile.mkdir();
                }

                scanDirectory(file, sourceDirectoryPath, destDirectoryPath, sourceFileEncoding, destFileEncoding);
            } else {
                System.out.println("Source file:\t" + file.getAbsolutePath()
                        + "\nDest file:\t" + (destDirectoryPath + file.getAbsolutePath().substring(sourceDirectoryPath.length()))
                        + "\n-----------------------------------------------------------------");

                convertFile(file.getAbsolutePath(), destDirectoryPath + file.getAbsolutePath().substring(sourceDirectoryPath.length()),
                        sourceFileEncoding, destFileEncoding);
            }
        }
    }

    /**
     * @param sourceFilePath     sourceFilePath
     * @param destFilePath       destFilePath
     * @param sourceFileEncoding sourceFileEncoding
     * @param destFileEncoding   destFileEncoding
     */
    private static void convertFile(String sourceFilePath, String destFilePath,
                                    String sourceFileEncoding, String destFileEncoding) {
        InputStream in = System.in;
        OutputStream out = System.out;

        Reader reader = null;
        Writer writer = null;

        try {
            //set up byte streams
            if (sourceFilePath != null) {
                in = new FileInputStream(sourceFilePath);
            }

            if (destFilePath != null) {
                out = new FileOutputStream(destFilePath);
            }

            // Use default encoding if no encoding is specified.
            if (sourceFileEncoding == null)
                sourceFileEncoding = System.getProperty("file.encoding");
            if (destFileEncoding == null)
                destFileEncoding = System.getProperty("file.encoding");

            // Set up character stream
            reader = new BufferedReader(new InputStreamReader(in, sourceFileEncoding));
            writer = new BufferedWriter(new OutputStreamWriter(out, destFileEncoding));

            char[] buffer = new char[BUFFER_SIZE];
            int len;
            while ((len = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();

        } finally {
            if (writer != null) {
                try {
                    writer.flush();
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        String sourceDirectoryPath = "D:\\Lee\\Products\\insquery\\src";
        String destDirectoryPath = "D:\\Lee\\Products\\insquery\\dest";

        scanDirectory(sourceDirectoryPath, destDirectoryPath, FILE_ENCODING_GBK, FILE_ENCODING_UTF8);
    }
}

 

分享到:
评论

相关推荐

    FileEncodingConverter文件编码转换工具

    【FileEncodingConverter文件编码转换工具】是一款高效实用的软件,主要功能是帮助用户解决不同文件在编码格式上遇到的问题。在计算机科学中,文件编码决定了文本文件中的字符如何被存储和解析,不同的编码方式(如...

    用java修改文件的编码

    public class FileEncodingConverter { public static void main(String[] args) { String sourceFilePath = "source.txt"; String targetFilePath = "target.txt"; String sourceEncoding = "GBK"; // 源文件...

    文件编码工具合集(四个经典工具)

    1. **FileEncodingConverter**:这可能是一款专门用于文件编码转换的应用程序,它能帮助用户将一个文件或一组文件从一种编码格式(如UTF-8、GBK等)转换为另一种编码格式。这样的工具对于处理中文、日文、韩文等非...

    java文件编码转换

    public class FileEncodingConverter { public static void convertGBKtoUTF8(String sourceFilePath, String targetFilePath) throws IOException { FileInputStream fis = new FileInputStream(sourceFilePath);...

    windows下ascii unicode utf8 相互转换

    3. 使用第三方工具:还有一些专门的编码转换软件,如“File Encoding Converter”等,提供了图形化的用户界面,操作更加直观。 在进行编码转换时,要特别注意以下几个要点: 1. 转换前备份:始终在转换前备份原始...

    文件编码转换器(FileConverter)源码.rar

    在FileConverter中,开发者利用了System.Text命名空间下的类,如Encoding类,它可以识别并操作各种编码类型。编码识别通常通过分析文件头的字节序列来判断,例如,UTF-8文件的前三个字节是0xEF, 0xBB, 0xBF,而ASCII...

    CodeDetector:检测文件编码,现在支持UTF-8,GBK,UTF-16LE,UTF-16BE,Java

    CodeDetectorDetect File encoding, Now support UTF-8,GBK,UTF-16LE,UTF-16BE, Java检查文件编码,主要针对中文文本。目前支持 UTF-8(BOM或无BOM)、GBK、UTF-16LE、UTF-16BE单个文件或文件夹,支持编码批量转换。...

    Aiseesoft PDF Converter Ultimate 3.3.8

    很实用,pdf 转换到 Doc 可以保持 Pdf 原来的排版布局。 Convert PDF to Word, PowerPoint...Set the layout and text encoding of the output file and output image’s format, color, size, resolution and quality.

    使用eclipse插件批量将Java源代码文件的编码从GBK(或其他编码)转为UTF-8

    1. **安装插件**:首先,我们需要在Eclipse Marketplace中搜索并安装支持批量转换编码的插件,如“Encoding Manager”或“Character Encoding Converter”。这些插件能够帮助用户方便地更改工程中所有文件的编码格式...

    HtmlToPdf、wkhtmltopdf工具

    converter.Document.Options.Web.DefaultEncoding = "UTF-8"; converter.AddPage(new PageSettings { Url = "http://example.com" }); var pdfData = converter.GeneratePdf(); File.WriteAllBytes("output.pdf...

    eclipse转码插件.zip

    4. 在右侧的“Text file encoding”下拉框中,选择“Other”,然后在弹出的列表中选择“UTF-8”。 5. 点击“Apply and Close”保存设置。 但是,这仅改变项目默认的编码设置,不会实际转换文件内容。为了实现文件...

    DjVu的阅读软件!!!!!!!!!!

    The quality of the new image file can be manipulated, using options that affect the high contrast portions ('text', for DjVu images) and the background (the rest of the image) in different ways, ...

    使用ANT把一种编码格式转化为另外一种编码格式

    <project name="EncodingConverter" default="convert" basedir="."> <!-- 定义属性 --> <property name="encoding.from" value="GBK"/> <property name="encoding.to" value="UTF-8"/> <!-- 清理旧目标 -...

    Java使用poi将word转换为html

    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "http://www.w3.org/TR/html4/strict.dtd"); transformer.setOutputProperty...

    中文乱码现象和解决方案

    InputStreamReader reader = new InputStreamReader(new FileInputStream("file.txt"), "GBK"); ``` ##### 2. 设置JVM默认编码 通过修改JVM启动参数,可以改变默认字符集。这对于整个应用来说是一个全局设置。 ```...

    java中文乱码问题处理集锦

    ByteToCharConverter converter = ByteToCharConverter.getConverter(encoding); char c[] = converter.convertAll(b); for (int i = 0; i ; i++) { System.out.println(Integer.toHexString(c[i])); } // 字符到...

    FastReport.v4.15 for.Delphi.BCB.Full.Source企业版含ClientServer中文修正版支持D4-XE5

    + added converter from Rave Reports ConverterRR2FR.pas + added Cross.KeepRowsTogether property + optimised merging cells in the BIFF export + added property DataOnly to exports + pictures format in ...

    python如何提取英语pdf内容并翻译

    with open(ENtextfile, 'w', encoding='utf-8') as en_file: en_file.write(pdf_text) if isTranslate: with open(CNtextfile, 'w', encoding='utf-8') as cn_file: for line in pdf_text.splitlines(): ...

    PS4 SDK Document

    Library for PC OverviewATRAC9™ Library for PC ReferenceATRAC9™ Seamless Playback TutorialATRAC9™ File FormatAudiodecCpu Library OverviewAudiodecCpu Library ReferenceAudiodec Library ...

Global site tag (gtag.js) - Google Analytics