首页 > 编程语言 >java 转换指定文件夹文件编码工具

java 转换指定文件夹文件编码工具

时间:2022-12-30 11:24:20浏览次数:53  
标签:编码 java osw next 文件夹 close new null public

import java.io.*;

public class test {
    public static void main(String[] args) {
        printFiles(new File("./src"), 1);
    }

    public static void printFiles(File dir, int tab) {
        if (dir.isDirectory()) {
            File next[] = dir.listFiles();
            for (int i = 0; i < next.length; i++) {
                for (int j = 0; j < tab; j++) {
                    System.out.print("|--");
                }
                try {
                    if(next[i].isFile()) {
                        convert(next[i].getPath());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }

                System.out.println(next[i].getName());
                if (next[i].isDirectory()) {
                    printFiles(next[i], tab + 1);
                }
            }
        }
    }

    public static void convert(String inputfile) throws IOException {
        StringBuffer sb = new StringBuffer();

        InputStreamReader isr = null;
        OutputStreamWriter osw = null;

        isr = new InputStreamReader(new FileInputStream(inputfile), "GB2312");
//        }
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        while ((line = br.readLine()) != null) {
            sb.append(line + "\n");
        }
        br.close();
        isr.close();

        osw = new OutputStreamWriter(new FileOutputStream(inputfile), "UTF-8");

        BufferedWriter bw = new BufferedWriter(osw);
        bw.write(sb.toString());
        bw.close();
        osw.close();
    }
}

  

标签:编码,java,osw,next,文件夹,close,new,null,public
From: https://www.cnblogs.com/zz4926/p/17014414.html

相关文章