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



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 " +
// "@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; " +
// "import com.fasterxml.jackson.databind.annotation.JsonSerialize; " +
// "import tz.lion.ds.annotation.FormFileds; " +
// "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) + " ");
flag = true;
} else
writer.write(str + " ");
}

is.close();

writer.flush();
writer.close();

if (flag) {
file.delete();
tmpfile.renameTo(new File(file.getAbsolutePath()));
} else
tmpfile.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}

原文地址:https://www.cnblogs.com/CaptainLin/p/12696354.html