zoukankan      html  css  js  c++  java
  • Java实现将GBK编码格式的文件夹中所有文件都转化为UTF-8格式的文件,编码格式转化

     package CodeConvert;
    
    
    import Java.io.BufferedReader;
    
    import java.io.BufferedWriter;
    
    import java.io.File;
    
    import java.io.FileInputStream;
    
    import java.io.FileOutputStream;
    
    import java.io.InputStreamReader;
    
    import java.io.OutputStreamWriter;
    
    import java.nio.charset.Charset;
    
    
    public class GBKtoUTF8 {
    
    //遍历文件
    
    public static void fileList(File file) {
    
    File rootFile = file;
    
    File[] files = rootFile.listFiles();
    
    if (files != null) {
    
    for (File f:files) {
    
    if (!f.isDirectory()) {
    
    codeConvert(f);
    
    }
    
    System.out.println(f.getPath());
    
    fileList(f);//递归调用子文件夹下的文件
    
    }
    
    }
    
    }
    
    //另一种展示方式,自己玩的与实现功能无关
    
    public static void fileList1(File file,int node){
    
    node++;
    
    File rootFile = file;
    
    File[] files = rootFile.listFiles();
    
    if (files != null) {
    
    for(File f:files){
    
    for (int i=0; i < node; i++) {
    
    if (i == node-1) {
    
    System.out.print("|-");
    
    }else System.out.print(" ");
    
    }
    
    System.out.println(f.getName());
    
    fileList1(f, node);
    
    }
    
    }
    
    }
    
    
    public static void main(String[] args){
    
    File file = new File("/Users/...../GBK");
    
    GBKtoUTF8.fileList(file);
    
    //GBKtoUTF8.codeConvert(file);
    
    }
    
    public static void  codeConvert(File file) {
    
    try{
    
    BufferedReader br = new BufferedReader(new InputStreamReader
    
    (new FileInputStream(file), Charset.forName("GBK")));
    
    StringBuilder sb = new StringBuilder();
    
    String str;
    
    while ((str = br.readLine())!= null) {
    
    sb.append(str);
    
    sb.append("
    ");
    
    }
    
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter
    
    (new FileOutputStream(file),Charset.forName("UTF-8")));
    
    bw.write(sb.toString());
    
    bw.flush();
    
    bw.close();
    
    //br.close();
    
    }catch (Exception e){
    
    e.printStackTrace();
    
    }
    
    }
    
    }
  • 相关阅读:
    19年春第六周学习
    Java编程思想
    个人作业4-结对开发地铁
    Hadoop学习笔记—1.基本介绍与环境配置
    ZooKeeper学习第二期--ZooKeeper安装配置
    ZooKeeper学习第一期---Zookeeper简单介绍
    loadrunner--参数化—使用数据文件参数化
    loadrunner--analysis--图表筛选、合并、显示
    loadrunner--并发测试
    session和cookie
  • 原文地址:https://www.cnblogs.com/stsinghua/p/6418709.html
Copyright © 2011-2022 走看看