/**
* 同步两个文件夹中的内容工具类
*/
package com.cn.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import org.apache.commons.io.FileUtils;
/**
* @Title: FileUtilsUpdate.java
* @Description:
* @author 圣光帕拉丁
* @date 2021-11-12 09:15:35
*/
public class FileUtilsUpdate {
/**
* 同步两个文件夹中的所有文件
* @param f1 以这个文件中的内容为主同步到f2的内容
* @param f2 要同步的文件夹
* @throws IOException
*/
public int[] updateFile(File f1,File f2,int [] num) throws IOException{
//确保f2,f2存在,并且都是文件夹目录
if(f1.exists()&&f2.exists()&&f1.isDirectory()&&f2.isDirectory()){
File[] listFiles = f1.listFiles();
//String f1_base_path = f1.getAbsolutePath();//f1的绝对路径
String f2_base_path = f2.getAbsolutePath();//f2的绝对路径
//遍历所有f1中的内容,获取所有要更新同步的复制的文件夹路径
for (File file : listFiles) {
/*
* 如果是文件夹,就到f2路径下判断是否存在这样的文件夹,
* 如果不存在就整个文件夹复制
* 如果存在就递归,然后进一步判断里面的文件情况
*/
//先获取对应的镜像目录
String name = file.getName();
File file2 = new File(f2_base_path,name);
if(file.isDirectory()){
//如果文件夹存在,判断两个文件夹大小是否相同,就递归
//如果不相同就递归
if(file2.exists()){
long size1 = FileUtils.sizeOfDirectory(file);
long size2 = FileUtils.sizeOfDirectory(file2);
//当两个文件夹的大小不相同说明里面有文件更新了,需要同步调用递归
if(size1!=size2){
updateFile(file,file2,num);
}
}else{//如果不存在了就复制
FileUtils.copyDirectory(file, file2);
System.out.println("同步了文件夹:"+file.getAbsolutePath());
num[0]=num[0]+1;
}
}else{//是文件
if(file2.exists()){//如果文件存在再判断大小是否相同
long length = file.length();
long length2 = file2.length();
if(length!=length2){//说明文件已经修改了
FileUtils.copyFile(file, file2);
System.out.println("同步了文件:"+file.getAbsolutePath());
num[0]=num[0]+1;
}
}else{//文件不存在,就复制到
FileUtils.copyFile(file, file2);
System.out.println("同步了文件:"+file.getAbsolutePath());
num[0]=num[0]+1;//第复制一个文件夹就累计加1
}
}
}
}
return num;
}
/**
* 同步两个文件的文件两个文件对象
* @param f1 文件对象
* @param f2 文件对象
* @return
* @throws IOException
*/
public static int doUpdate(File f1,File f2) throws IOException{
int [] intArr={0};
int [] updateFile = new FileUtilsUpdate().updateFile(f1,f2,intArr);
return updateFile[0];
}
/**
* 同步两个文件的文件两个文件字符串路径
* @param path1 文件字符串路径
* @param path2 文件字符串路径
* @return
* @throws IOException
*/
public static int doUpdate(String path1,String path2) throws IOException{
int [] intArr={0};
int [] updateFile = new FileUtilsUpdate().updateFile(new File(path1),new File(path2),intArr);
return updateFile[0];
}
}

这是javaswing界面代码,目录反转是对调两个文本框内容
从而实现双向同步。
/**
*
*/
package com.cn.swing;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import com.cn.utils.FileUtilsUpdate;
import com.cn.utils.RobotUtils;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.awt.event.ActionEvent;
/**
* @Title: FileUpdateUI.java
* @Description:
* @author 圣光帕拉丁
* @date 2021-11-12 02:46:48
*/
public class FileUpdateUI extends JFrame {
private JPanel contentPane;
private JTextField text1;
private JTextField text2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
FileUpdateUI frame = new FileUpdateUI();
frame.setVisible(true);
frame.setLocationRelativeTo(null);
// 添加图标
ImageIcon imageIcon = new ImageIcon("./图标/放大镜.png");
frame.setIconImage(imageIcon.getImage());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public FileUpdateUI() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 481, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
text1 = new JTextField();
text1.setColumns(10);
text2 = new JTextField();
text2.setColumns(10);
JLabel label = new JLabel("目标文件");
JLabel label_1 = new JLabel("同步到=>");
JButton button = new JButton("同步目录");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
long time1 = System.currentTimeMillis();
// 这里设置提示在标题中
Thread tt = new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int i = 0;
String str="用时:";
while (true) {
if (i % 4 == 1) {
setTitle("正在同步文件中耗时:"+str+i+"秒");
} else if (i % 4 == 2) {
setTitle("正在同步文件中耗时:"+str+i+"秒");
} else if (i % 4 == 3) {
setTitle("正在同步文件中耗时:"+str+i+"秒");
} else {
setTitle("正在同步文件中耗时:"+str+i+"秒");
}
i++;
RobotUtils.mydelay(1000);
}
}
});
tt.start();
String t1 = text1.getText();
String t2 = text2.getText();
try {
int doUpdate = FileUtilsUpdate.doUpdate(t1, t2);
if(tt.isAlive()){
tt.stop();
}
long time2 = System.currentTimeMillis();
long sec=(time2-time1)/1000;
setTitle("同步成功:" + doUpdate + "个文件,耗时:"+sec+"秒");
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(tt.isAlive()){
tt.stop();
}
}
}
});
JButton button_1 = new JButton("清空目录");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
text1.setText("");
text2.setText("");
text1.requestFocus();// 获取焦点
}
});
JButton button_2 = new JButton("目录反转");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String t1 = text1.getText();
String t2 = text2.getText();
text1.setText(t2);
text2.setText(t1);
}
});
GroupLayout gl_contentPane = new GroupLayout(contentPane);
gl_contentPane.setHorizontalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addComponent(label)
.addComponent(label_1))
.addGap(27)
.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addComponent(button)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(button_2, GroupLayout.PREFERRED_SIZE, 89, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.UNRELATED)
.addComponent(button_1))
.addComponent(text2)
.addComponent(text1, GroupLayout.DEFAULT_SIZE, 269, Short.MAX_VALUE))
.addContainerGap(99, GroupLayout.PREFERRED_SIZE))
);
gl_contentPane.setVerticalGroup(
gl_contentPane.createParallelGroup(Alignment.LEADING)
.addGroup(gl_contentPane.createSequentialGroup()
.addContainerGap()
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(text1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label))
.addGap(29)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(text2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
.addComponent(label_1))
.addGap(40)
.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
.addComponent(button)
.addComponent(button_2)
.addComponent(button_1))
.addContainerGap(108, Short.MAX_VALUE))
);
contentPane.setLayout(gl_contentPane);
}
}