java复制文件 覆盖文件_java 工具类 复制文件目录 批量修改替换文件

这个Java程序展示了如何复制一个文件夹,并在复制过程中批量修改和替换文件内容。它首先定义了一个`moveMain`类,用于复制文件夹,并在目标路径创建新的目录。然后,`Modify`类用于遍历指定目录下的所有文件,查找并替换特定的字符串。通过`opeationDirectory`和`operationFile`方法,程序实现了递归处理子目录和文件的替换操作。
摘要由CSDN通过智能技术生成

import java.io.*;

public class moveMain {

public static void main(String[] args) throws Exception {

// String path="G:\\ideaWorkspace\\tzlion-data-platform-data-server\\src\\main\\java\\tz\\lion\\ds\\entity\\project\\monomer";

String destPath="G:\\ideaWorkspace\\tzlion-data-platform-data-server\\src\\main\\java\\tz\\lion\\ds\\entity\\out\\tmp";

// copyFile(path,destPath);

//

// File file = new File(destPath); //获取其file对象

// File[] fs = file.listFiles(); //遍历path下的文件和目录,放在File数组中

// for(File f:fs){ //遍历File[]数组

// if(!f.isDirectory()) {

// System.out.println(f);

// }

// }

new Modify(destPath, "import io.swagger.annotations.FormFileds;", "");

// new Modify(destPath, "ApiModelProperty", "FormFileds");

// new Modify(destPath, " extends PMProps", "Resp");

// new Modify(destPath, "@Data", "@Data\n" +

// "@JsonSerialize(using = FormXSerializer.class)");

// new Modify(destPath, "package tz.lion.ds.entity.project.monomer;", "package tz.lion.ds.entity.out.tmp;");

// new Modify(destPath, "import lombok.Data;", "import lombok.Data;\n" +

// "import com.fasterxml.jackson.databind.annotation.JsonSerialize;\n" +

// "import tz.lion.ds.annotation.FormFileds;\n" +

// "import tz.lion.ds.entity.out.serializer.FormXSerializer;");

}

//复制文件夹的方法,开始的时候是两个文件夹

public static void copyFile(String source,String destin) throws Exception //在这里直接抛出各种异常,就不一一处理了

{

File path1=new File(source);

File path2=new File(destin);

//如果源目录不存在,那就不用复制了,

if(path1.exists())

{

//Create destination folder,如果目标目录不存在,就创建目标目录,因为没有目录文件复制不过去的

if(!path2.exists())

{

path2.mkdirs();

}

//取得源目录下面的所有文件和文件夹

File[] items=path1.listFiles();

FileInputStream fis=null;

FileOutputStream fos=null;

//取得所有文件和文件夹之后,遍历处理,如果是文件,就复制文件,如果是文件夹,则递归文件夹下面的文件和文件夹

for(File item:items)

{

//如果是文件才进行复制

if(item.isFile())

{

//输入输出流的两个常用构造函数,其中在用来了一个字段File.separator,先用输入流读取文件,然后用输出流写文件到目标位置,完成复制功能

fis=new FileInputStream(item);

String fileName = item.getName();

String newFileName=fileName.substring(0,fileName.lastIndexOf(".") )+"Resp"+fileName.substring(fileName.lastIndexOf(".") );

fos=new FileOutputStream(path2+File.separator+newFileName);

byte[] b=new byte[1024];

for(int i=0;(i=fis.read(b))!=-1;)

{

fos.write(b,0,i);

fos.flush();

}

//close the Stream关闭资源啊,什么异常处理的就不写,自己补上吧

fos.close();

fis.close();

}

//如果是文件夹,递归文件夹

else

{

copyFile(item.getPath(),path2+File.separator+item.getName());

}

}

}

else

{

System.out.println("source path:"+source+" is not exists!");

}

}

}

class Modify {

private String path;

private final String target;

private final String newContent;

public Modify(String path, String target, String newContent) {

// 操作目录。从该目录开始。该文件目录下及其所有子目录的文件都将被替换。

this.path = path;

// target:需要被替换、改写的内容。

this.target = target;

// newContent:需要新写入的内容。

this.newContent = newContent;

operation();

}

private void operation() {

File file = new File(path);

opeationDirectory(file);

}

public void opeationDirectory(File dir) {

File[] files = dir.listFiles();

for (int i = 0; i < files.length; i++) {

File f = files[i];

if (f.isDirectory())

// 如果是目录,则递归。

opeationDirectory(f);

if (f.isFile())

operationFile(f);

}

}

public void operationFile(File file) {

try {

InputStream is = new FileInputStream(file);

BufferedReader reader = new BufferedReader(

new InputStreamReader(is));

String filename = file.getName();

// tmpfile为缓存文件,代码运行完毕后此文件将重命名为源文件名字。

File tmpfile = new File(file.getParentFile().getAbsolutePath()

+ "\\" + filename + ".tmp");

BufferedWriter writer = new BufferedWriter(new FileWriter(tmpfile));

boolean flag = false;

String str = null;

while (true) {

str = reader.readLine();

if (str == null)

break;

if (str.contains(target)) {

writer.write(str.replace(target,newContent) + "\n");

flag = true;

} else

writer.write(str + "\n");

}

is.close();

writer.flush();

writer.close();

if (flag) {

file.delete();

tmpfile.renameTo(new File(file.getAbsolutePath()));

} else

tmpfile.delete();

} catch (Exception e) {

e.printStackTrace();

}

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值