java定时复制文件夹及文件,可视化界面(递归)
package swing;
import java.awt.Button;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.nio.channels.FileChannel;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
public class FileCopy extends Frame implements ActionListener{
private static final long serialVersionUID = 1L;
public int rate = 0;
String oldFilepath;
String newFilepath;
private JProgressBar pro = new JProgressBar();
JPanel frame;
Button copy = new Button("Copy");
JTextArea FromPath = new JTextArea("");
JTextArea ToPath = new JTextArea("");
Run A = new Run();
FileCopy(){
frame = new JPanel();
frame.setLayout(null);
final JLabel j1 = new JLabel("此地址开始复制:");
final JLabel j2 = new JLabel("复制至此地址中:");
j1.setBounds(0, 0, 100, 100);
FromPath.setBounds(100, 40, 300, 40);
j2.setBounds(0, 100, 100, 100);
ToPath.setBounds(100, 140, 300, 40);
copy.setBounds(400, 100, 80, 40);
pro.setBounds(50, 200, 400, 40);
pro.setStringPainted(true);
copy.addActionListener(this);
frame.add(j1);
frame.add(FromPath);
frame.add(ToPath);
frame.add(j2);
frame.add(copy);
frame.add(pro);
this.add(frame);
A.start();
}
public void copy(String sourcePath, String destPath){
try {
File oldFile = new File(sourcePath);
File newFile = new File(destPath);
if(!oldFile.exists()){
System.out.println("请检查您的源路径是否合法!");
}
File[] listFiles = oldFile.listFiles();
for(File f : listFiles) {
System.out.println(f.getName());
if(f.isFile()) {
CopyDocuments.copyDocuments(f.getPath(),newFile.getPath());
}else if(f.isDirectory()) {
File nextFile = new File(newFile.getPath() + File.separator + f.getName());
nextFile.mkdir();
copy(oldFile+"\\"+f.getName(),newFile+"\\"+f.getName());
}
}
}catch (Exception e) {
System.out.println("文件复制发生错误!" + e);
}
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == copy) {
oldFilepath = FromPath.getText();
newFilepath = ToPath.getText();
System.out.println(oldFilepath);
System.out.println(newFilepath);
}
}
private class Run extends Thread {
public void run() {
while(true) {
System.out.println("启动...");
if(oldFilepath!=null) {
try {
copy(oldFilepath, newFilepath);
} catch (Exception e) {
e.printStackTrace();
}
}
try{
Thread.sleep(200000);
}catch(Exception e) {
System.out.println(e);
}
}
}
}
public static void main(String[] args) {
FileCopy copy = new FileCopy();
copy.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
copy.setBounds(0,0,500,300);
copy.setVisible(true);
copy.setLayout(null);
copy.setResizable(false);
}
}
文件复制
package swing;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CopyDocuments {
public static void main(String[] args) throws Exception {
copyDocuments("E:\\2","E:\\1");
}
@SuppressWarnings("resource")
public static boolean copyDocuments(String SourcePath, String DestPath) {
File oldFile = new File(SourcePath);
File newFile = new File(DestPath);
if (!oldFile.exists() || oldFile.isDirectory()) {
return false;
}
if (!newFile.exists()) {
newFile.mkdirs();
}
String oldFileName = oldFile.getName();
Pattern suffixPattern = Pattern.compile("\\.\\w+");
Matcher matcher = suffixPattern.matcher(oldFileName);
String nameBody;
String suffix;
if (matcher.find()) {
nameBody = oldFileName.substring(0, matcher.start());
suffix = oldFileName.substring(matcher.start());
} else {
nameBody = oldFileName;
suffix = "";
}
int fileNumber = 0;
File newFile1 = new File(DestPath, oldFileName);
while (newFile1.exists()) {
fileNumber++;
String newFileName = nameBody + "-copy" + fileNumber + suffix;
newFile1 = new File(newFile1, newFileName);
}
try {
FileChannel fileIn = new FileInputStream(oldFile).getChannel();
FileChannel fileOut = new FileOutputStream(newFile1).getChannel();
fileIn.transferTo(0, fileIn.size(), fileOut);
fileIn.close();
fileOut.close();
} catch (IOException e) {
return false;
}
return true;
}
}