将某个路径的文本文件批量复制并更改其中的一些内容

package net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
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;

/**
 *
 * 将某个路径的文本文件批量复制并更改其中的一些内容
 *
 */
public class ChangeFile {
    private static int len ;
    private static String resultPath;
    private static boolean change = false;
    public static void main(String[] args){
        String path = "";
        String result = "";
        change(path,result);
    }
    public static void change(String path,String result){
        File foder = new File(path);
        if(!foder.exists()){
            System.out.println("目标目录不存在????");
            return;
        }
        len = path.length();
        resultPath = result;
        md(resultPath);
        tree(foder);
    }
    //根据路径创建文件夹
    private static void md(String path){
        File folder = new File(path);
        if(!folder.exists()){
            folder.mkdir();
        }
    }
    //根据路径创建文件
    private static File mf(String path){
        File file = new File(path);
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }
    //遍历文件夹
    private static void tree(File file){
        if(file.isDirectory()){
            String p = file.getAbsolutePath();
            int l = p.length();
            if(l>len){//创建新目录
                String np = p.substring(len);
                md(resultPath+np);
            }
            File[] fileList = file.listFiles();
            for(File f:fileList){
                tree(f);
            }
        }else if(file.isFile()){//复制文件
            change = false;
            String fp = file.getAbsolutePath();
            if(fp.endsWith(".java")){
                change = true;
            }
            String nf = fp.substring(len);
            File sfile = mf(resultPath+nf);
            try {
                BufferedReader r = new BufferedReader(new InputStreamReader(
                        new FileInputStream(file)));
                
                BufferedWriter w = new BufferedWriter(new OutputStreamWriter(
                        new FileOutputStream(sfile)));
                String line = null;
                while((line=r.readLine())!=null){
                    String en = changeStr(line);
                    w.write(en+"\n");
                }
                r.close();
                w.flush();
                w.close();
                
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            
        }
    }
    //每一行的替换方法
    private static String changeStr(String str){
        String end = str;
        if(change){
            //具体规则
            end = str;
            
            
            
        }
        return end;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值