package com.zyx.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
/**
* @ClassName: CopyFileToPath 复制某一路径下的所有sql文件到另一目录下
* @Description:
* @Author zyx
* @Date:Create: 2021/12/29 16:46
*/
public class CopyFileToPath {
static String sqlFile =".sql";
static String fileName = "autoRun.sql";
static String orgPath = "C:\\Users\\zyx\\Desktop\\sql";
static String desPpath = "D:\oracle\\product_patch";
public static void main(String[] args) throws IOException {
getFiles(orgPath,desPpath);
}
public static void getFiles(String orgPath,String desPpath) throws IOException {
File file = new File(orgPath);
// 如果这个路径是文件夹
if (file.isDirectory()) {
// 获取路径下的所有文件
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
// 如果还是文件夹 递归获取里面的文件 文件夹
if (files[i].isDirectory()) {
getFiles(files[i].getPath(),desPpath);
} else {
String name = files[i].getName();
if(!name.endsWith(sqlFile) ||fileName.equals(name)){
continue;
}
String pathName = desPpath + "\\" + name;
File desFile = new File(pathName);
if(desFile.exists()){
desFile.delete();
}
usingFileChannelsCopyFile(files[i],desFile);
System.out.println(name+"复制完成");
}
}
} else {
String name = file.getName();
if (name.endsWith(sqlFile)) {
String pathName = desPpath + "\\" + name;
File desFile = new File(pathName);
if(desFile.exists()){
desFile.delete();
}
usingFileChannelsCopyFile(file,desFile);
}
}
}
public static void usingFileChannelsCopyFile(File source, File dest) throws IOException {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(dest).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}
}