zoukankan      html  css  js  c++  java
  • java 工程编码格式由GBK转化成utf-8 (转载)

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.UnsupportedEncodingException;
    import java.util.Scanner;
    
    /**
     * 把gbk编码的程序变换为用utf-8的格式编码
     *
     * 此程序只是为了改变 .java文件的编码格式如果你想要变换为其他格式只需要改变下面对应的编码按格式
     *
     * @author ylg
     */
    public class Files {
    
        /**
         *
         * @param args
         * @throws UnsupportedEncodingException
         * @throws IOException
         */
        public static void main(String[] args) throws UnsupportedEncodingException, IOException {
            Scanner scan = new Scanner(System.in);
            System.out.println("请输入需要改变编码格式的文件位置");
            String str = scan.nextLine();
            File file = new File(str);
            System.out.println("文件的初始编码");
            String bm1 = scan.nextLine();
            System.out.println("文件需要转换成的编码");
            String bm2 = scan.nextLine();
            getAllFiles(file, bm1, bm2);
        }
    
        /**
         *
         * @param file 要编译的文件
         * @param bm1 文件的初始编码
         * @param bm2 文件需要转换成的编码
         * @throws FileNotFoundException 文件找不到
         * @throws UnsupportedEncodingException 编码出错
         * @throws IOException io异常
         */
        public static void getAllFiles(File file, String bm1, String bm2) throws FileNotFoundException, UnsupportedEncodingException, IOException {
            if (file.isDirectory()) {
                File[] test = file.listFiles();
                for (File test1 : test) {
                    //类的名字
                    String str = test1.getPath();
                    if (str.endsWith("java") & test1.isFile()) {
                        String[] s = str.split("\.");
                        String filecope = s[0] + "cope." + s[1];
                        System.out.println(filecope);
                        File fil = new File(filecope);
                        //转格式
                        InputStreamReader isr = new InputStreamReader(new FileInputStream(test1), bm1);
                        OutputStreamWriter osr = new OutputStreamWriter(new FileOutputStream(fil), bm2);
                        int re = -1;
                        while ((re = isr.read()) != -1) {
                            osr.write(re);
                        }
                        isr.close();
                        osr.close();
                        InputStreamReader isrr = new InputStreamReader(new FileInputStream(fil), bm2);
                        OutputStreamWriter osrw = new OutputStreamWriter(new FileOutputStream(test1), bm2);
                        int r = -1;
                        while ((r = isrr.read()) != -1) {
                            osrw.write(r);
                        }
                        isrr.close();
                        osrw.close();
                        boolean d = fil.delete();
                        System.out.println(str + "文件转换utf-8成功:" + d);
                    }
                    getAllFiles(test1, bm1, bm2);
                }
            }
        }
    
    }
  • 相关阅读:
    JS中使用正则表达式封装的一些常用的格式验证的方法-是否外部url、是否小写、邮箱格式、是否字符、是否数组
    Java中操作字符串的工具类-判空、截取、格式化、转换驼峰、转集合和list、是否包含
    Cocos2d-x 2.0 自适应多种分辨率
    应用自定义移动设备外观
    为移动设备应用程序创建外观
    【2020-11-28】人生十三信条
    【2020-11-27】事实证明,逃避是下等策略
    Python 之web动态服务器
    Python 之pygame飞机游戏
    PHP 之转换excel表格中的经纬度
  • 原文地址:https://www.cnblogs.com/alasijia/p/9336041.html
Copyright © 2011-2022 走看看