java的代码编码转换

package ch03;

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;


import org.junit.Test;

public class TestEncodeConverter {

 @Test
 public void testEncodeConverter() {
  String sourcePath = "D:\\code\\szgr\\ruledrools_bak\\src";
  String destPath = "d:\\converter\\src";
  File file = new File(sourcePath);
  convert(file, sourcePath, destPath);
  System.out.println("----------------------------------------------------------------------------------------------------------------------");
    
  sourcePath = "D:\\code\\szgr\\ruledrools_bak\\test";
  destPath = "d:\\converter\\test";
  file = new File(sourcePath);
  convert(file, sourcePath, destPath);
 }
    private void createDirectory(File file){
     if(file.exists()){
      System.out.println("true");
      return;
     }
     File parentFile = file.getParentFile();
     if(!parentFile.exists()){
      createDirectory(parentFile);
      file.mkdir();
     }else{
      file.mkdir();
     }
    }
 // D:\code\szgr\ruledrools_bak\src\com\szgr\ruledrools\bean\converter\basic\StringConverter.java
 private void convert(File file, String sourcePath, String destPath) {
  if (file.isDirectory()) {
   File[] files = file.listFiles();
   for (File f : files) {
    convert(f, sourcePath, destPath);
   }
   // System.out.println(file.getAbsolutePath());
  } else {

   if (file.getName().endsWith(".java")
     || file.getName().endsWith(".properties") || file.getName().endsWith(".xml")
     || file.getName().endsWith(".drl")) {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
     fis = new FileInputStream(file);
     InputStreamReader reader = new InputStreamReader(fis, "GBK");
     BufferedReader br = new BufferedReader(reader);

     String endPath = file.getAbsolutePath().substring(
       sourcePath.length());
     endPath = endPath.startsWith("\\") ? endPath : "\\"+endPath;
     String newFileName = destPath
       + endPath;
     
     File f = new File(newFileName);
     File parentFile = f.getParentFile();
     System.out.println(newFileName+"="+parentFile);
     createDirectory(parentFile);
     if (!f.exists()) {
       f.createNewFile();
     }
     
     
      fos = new FileOutputStream(f);
        
      OutputStreamWriter writer = new
      OutputStreamWriter(fos,"utf-8");
      BufferedWriter bw = new BufferedWriter(writer);
        
      String line = null;
      while((line = br.readLine()) != null){
      bw.write(line+"\r\n");
      }
      bw.flush();
      fos.close();
      fis.close();

    } catch (Exception e) {
     e.printStackTrace();
    } finally {
     if (fis != null) {
      try {
       fis.close();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }
     if (fos != null) {
      try {
       fos.close();
      } catch (IOException e) {
       e.printStackTrace();
      }
     }
    }
   }

  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值