批量过滤拷贝
因为工作需要抽取大量数据,自己捣鼓了一个这样的批量过滤拷贝工具,实现以下几种拷贝功能:
(1)全量拷贝:拷贝源始路径下所有数据到目标路径。
(2)过滤拷贝:根据过滤文件(内容为一行为一个文件名),拷贝出其中的文件(包含文件所在的各层目录文件夹)
(3)过滤拷贝(不含各层目录):根据过滤文件(内容为一行为一个文件名),直接拷贝其中的文件到目标路径
(4)目录过滤拷贝:根据过滤文件(内容为一行为一个目录名称),将源始路径下一层目录名称相同的数据拷贝到目标路径
注:过滤文件内容一行保存一个名称,拷贝的是源路径下与其名称相同的文件或者目录数据。
编程语言:Java
图形化界面工具包:Swing
通过构造函数初始化程序图形界面
JPanel jP_src = new JPanel();
JPanel jP_target = new JPanel();
JPanel jP_filter_file = new JPanel();
JPanel jP_full_filter = new JPanel();
JPanel jP_filter_only = new JPanel();
JPanel jP_filter_dir = new JPanel();
JLabel jL_src = new JLabel("源始路径:");
JLabel jL_target = new JLabel("目标路径:");
JLabel jL_filter_file = new JLabel("过滤文件:");
final JTextField jT_src = new JTextField(15);
final JTextField jT_target = new JTextField(15);
jT_filter_file = new JTextField(15);
JButton jButton_src = new JButton("选择...");
JButton jButton_target = new JButton("选择...");
JButton jButton_filter_file = new JButton("选择...");
JButton jButton_full = new JButton("全量拷贝");
JButton jButton_filter = new JButton("过滤拷贝");
JButton jButton_filter_only = new JButton("过滤拷贝(不含各层目录)");
JButton jButton_filter_dir = new JButton("目录过滤拷贝");
jP_src.add(jL_src);
jP_src.add(jT_src);
jP_src.add(jButton_src);
jP_target.add(jL_target);
jP_target.add(jT_target);
jP_target.add(jButton_target);
jP_filter_file.add(jL_filter_file);
jP_filter_file.add(jT_filter_file);
jP_filter_file.add(jButton_filter_file);
jP_full_filter.add(jButton_full);
jP_full_filter.add(jButton_filter);
jP_filter_only.add(jButton_filter_only);
jP_filter_dir.add(jButton_filter_dir);
this.add(jP_src);
this.add(jP_target);
this.add(jP_filter_file);
this.add(jP_full_filter);
this.add(jP_filter_only);
this.add(jP_filter_dir);
// 设置窗口标题,大小,行列数
this.setTitle("批量过滤拷贝工具");
this.setSize(400, 300);
this.setLayout(new GridLayout(6, 1));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
各个拷贝选项按钮触发的监听事件
// 源始路径选择
jButton_src.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jFileChooser.showDialog(new JLabel(), "选择此源路径");
//设置并获取源始路径
jT_src.setText(jFileChooser.getSelectedFile().getAbsoluteFile().toString());
srcPath_select = jFileChooser.getSelectedFile().getAbsolutePath();
}
});
// 目标路径选择
jButton_target.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
jFileChooser.showDialog(new JLabel(), "选择此目标路径");
//设置并获取目标路径
jT_target.setText(jFileChooser.getSelectedFile().getAbsolutePath());
newPath_select = jFileChooser.getSelectedFile().getAbsoluteFile().toString();
}
});
// 选择过滤文件
jButton_filter_file.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser jFileChooser = new JFileChooser();
jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jFileChooser.showDialog(new Label(),"选择此文件");
jFileChooser.setDialogTitle("选择过滤文件");
//设置并获取过滤文件路径
jT_filter_file.setText(jFileChooser.getSelectedFile().getAbsolutePath());
filter_file = jFileChooser.getSelectedFile().getAbsoluteFile().toString();
System.out.println(filter_file);
}
});
// 全量拷贝监听事件
jButton_full.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
copy(srcPath_select, newPath_select, null);
} catch (IOException ioException) {
ioException.printStackTrace();
}
System.out.println("全量拷贝完成,共拷贝" + size + "个文件");
}
});
// 过滤拷贝监听事件,这里参数使用匿名内部类匿名对象
jButton_filter.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
copy(srcPath_select, newPath_select, filter_file);
} catch (IOException ioException) {
ioException.printStackTrace();
}
System.out.println("过滤拷贝完成,共拷贝" + size + "个文件");
}
});
// 过滤拷贝(不含各层目录)监听事件,这里参数使用Lamda表达式
jButton_filter_only.addActionListener(e -> {
ArrayList<String> arraylist = null;
String newPath = null;
try {
//获取过滤文件名称集合
arraylist = getFilterfileAsArraylist(filter_file);
} catch (IOException ioException) {
ioException.printStackTrace();
}
copyDir_onlyFile(srcPath_select, newPath_select, arraylist);
});
// 目录过滤监听事件
jButton_filter_dir.addActionListener(e -> {
ArrayList<String> arraylist = null;
String newPath = null;
try {
//获取过滤文件名称集合,初始目标路径添加源路径最后一层目录
arraylist = getFilterfileAsArraylist(filter_file);
newPath = setTargetDir(srcPath_select, newPath_select);
} catch (IOException ioException) {
ioException.printStackTrace();
}
copyDir_filter_dir(srcPath_select, newPath, arraylist);
});
// 拷贝文件个数
private static int size;
// 全量拷贝
private static void copy(String srcPath, String newPath, String file_filter) throws IOException {
File srcDir = new File(srcPath);
if (srcDir !=null && srcDir.exists()) {
// 读取过滤文件,保存在集合arrayList
ArrayList<String> arrayList = getFilterfileAsArraylist(file_filter);
// 源路径最后一层目录加到初始目标路径
String newPath2 = setTargetDir(srcPath, newPath);
copyDir(srcPath, newPath2, arrayList);
} else {
return;
}
}
// 过滤拷贝(含文件所在各层目录文件夹)
private static void copyDir(String srcPath, String newPath2, ArrayList arrayList) {
File srcFile = new File(srcPath);
File newFile = new File(newPath2);
//参数srcPath最后一层目录或文件名称
String srcPathLastName = srcPath.substring(srcPath.lastIndexOf("\\") + 1);
//判断传入参数arrayList是否为空,不为空则实现过滤拷贝,否则为全量拷贝
if (arrayList != null) {
//判断文件名是否包含在过滤文件集合内
if (srcFile.isFile() && arrayList.contains(srcPathLastName)) {
System.out.println("**" + newFile.getParentFile());
//如果当前目标文件上一层目录不存在,则创建目录,否则报错
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdir();
}
copyFile(srcPath, newPath2);
size++;
} else if (srcFile.isDirectory()) {
//如果srcFile为目录,继续遍历
for (File file :
srcFile.listFiles()) {
System.out.println(file.getName());
copyDir(srcPath + "\\" + file.getName(), newPath2 + "\\" + file.getName(), arrayList);
}
}
} else {
copyDir_multi(srcPath, newPath2, null, srcFile);
}
}
// 过滤拷贝(不含文件所在各层目录文件夹)
private static void copyDir_onlyFile(String srcPath, String newPath2, ArrayList arrayList) {
if (arrayList != null) {
File srcFile = new File(srcPath);
String srcPathLastName = srcPath.substring(srcPath.lastIndexOf("\\") + 1);
if (srcFile.isFile() && arrayList.contains(srcPathLastName)) {
copyFile(srcPath, newPath2 + "\\" + srcPathLastName);
} else if (srcFile.isDirectory()) {
for (File file : srcFile.listFiles()) {
copyDir_onlyFile(srcPath + "\\" + file.getName(), newPath2, arrayList);
}
}
}
}
// 目录过滤拷贝
private static void copyDir_filter_dir(String srcPath, String newPath2, ArrayList arrayList) {
File srcFile = new File(srcPath);
File newFile = new File(newPath2);
for (File file : srcFile.listFiles()) {
if (file.isDirectory() && arrayList.contains(file.getName())) {
filter_dir_copy(srcPath + "\\" + file.getName(), newPath2 + "\\" + file.getName());
}
}
}
// 源路径最后一层目录加到目标路径
private static String setTargetDir(String srcPath, String newPath) {
String newPath2 = newPath + "\\" + srcPath.substring(srcPath.lastIndexOf("\\")+1);
File newDir = new File(newPath2);
if (!newDir.exists()) {
newDir.mkdir();
}
return newPath2;
}
// 读取过滤文件,保存在集合arrayList
private static ArrayList<String> getFilterfileAsArraylist(String file_filter) throws IOException {
ArrayList<String> arrayList = null;
String str;
if (file_filter != "" && file_filter != null) {
File file = new File(file_filter);
arrayList = new ArrayList<>();
FileInputStream fileInputStream = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
fileInputStream = new FileInputStream(file);
inputStreamReader = new InputStreamReader(fileInputStream, "gbk");
bufferedReader = new BufferedReader(inputStreamReader);
while ((str = bufferedReader.readLine()) != null) {
arrayList.add(str);
}
System.out.println(arrayList);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
bufferedReader.close();
}
if (inputStreamReader != null) {
inputStreamReader.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
}
}
return arrayList;
}
// 复制文件
private static void copyFile(String srcPath, String newPath2) {
File srcFile = new File(srcPath);
File newFile = new File(newPath2);
InputStream fin = null;
OutputStream fout = null;
try {
fin = new FileInputStream(srcFile);
fout = new FileOutputStream(newFile);
byte[] bytes = new byte[8192];
int len;
while ((len = fin.read(bytes)) != -1) {
fout.write(bytes, 0, len);
}
fout.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
最后,如果内容出现错误,欢迎指正或建议,一起学习进步!