zoukankan      html  css  js  c++  java
  • java读取文件并获得文件编码,转换为指定编码的工具类代码

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    
    public class Test {
        public static int fileCount = 0;
        public static String sourceFileRoot = "D:/workspace/test/src/com/zuidaima/"; // 将要转换文件所在的根目录
        public static String sourceCharset = "gbk"; // 源文件编码
        public static String targetCharset = "utf8"; // 目标文件编码
    
        public static void main(String[] args) throws IOException {
            File fileDir = new File(sourceFileRoot);
            convert(fileDir);
            System.out.println("Total Dealed : " + fileCount + "Files");
        }
    
        public static void convert(File file) throws IOException {
            // 如果是文件则进行编码转换,写入覆盖原文件
            if (file.isFile()) {
                // 只处理.java结尾的代码文件
                if (file.getPath().indexOf(".java") == -1) {
                    return;
                }
                InputStreamReader isr = new InputStreamReader(new FileInputStream(
                        file), sourceCharset);
                BufferedReader br = new BufferedReader(isr);
                StringBuffer sb = new StringBuffer();
                String line = null;
                while ((line = br.readLine()) != null) {
                    // 注意写入换行符
                    sb.append(line + "
    ");
                }
                br.close();
                isr.close();
    
                File targetFile = new File(file.getPath() + "." + targetCharset);
                OutputStreamWriter osw = new OutputStreamWriter(
                        new FileOutputStream(targetFile), targetCharset);
                BufferedWriter bw = new BufferedWriter(osw);
                // 以字符串的形式一次性写入
                bw.write(sb.toString());
                bw.close();
                osw.close();
    
                System.out.println("Deal:" + file.getPath());
                fileCount++;
            } else {
                for (File subFile : file.listFiles()) {
                    convert(subFile);
                }
            }
        }
    
    }
  • 相关阅读:
    linux tomcat 突然验证码出不来
    使用open live writer客户端写博客
    创建自己的maven模板
    Dynamic Web Module 3.0 requires Java 1.6 or newer
    win10 操作配置备忘
    Maven使用
    ORA-12514: TNS:listener does not currently know of service …
    PlantUML——4.实例演示1
    C语言基础(一)
    Linux系统挂载FAT32的U盘
  • 原文地址:https://www.cnblogs.com/gaopeng527/p/5155827.html
Copyright © 2011-2022 走看看