对某一文件进行恢复(Task1)
代码:
`package Task1;
import java.awt.Button;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.xml.stream.events.StartDocument;
public class Task1 extends JFrame{
static JTextField tf1 = new JTextField();
static JTextField tf2 = new JTextField();
public Task1(String title){
super(title);
setLayout(null);
JFrame jFrame = new JFrame("文件恢复");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(300,300);
setSize(500, 400);
Button button1 = new Button("选择待恢复文件");
button1.setBounds(new Rectangle(20, 20, 120, 25));
button1.addActionListener(new ChoosePartition());
Button button2 = new Button("选择恢复文件位置");
button2.setBounds(new Rectangle(20, 100, 120, 25));
button2.addActionListener(new ChooseLocota());
Button button3 = new Button("恢复文件");
button3.setBounds(new Rectangle(200, 200, 80, 25));
button3.addActionListener(new CopyFile());
tf1.setBounds(new Rectangle(150, 20, 250, 25));
tf2.setBounds(new Rectangle(150, 100, 250, 25));
add(button1);
add(button2);
add(button3);
add(tf1);
add(tf2);
setVisible(true);
}
public static void main(String[] args) {
Task1 t = new Task1("文件恢复");
}
}
class ChoosePartition implements ActionListener{
public void actionPerformed(ActionEvent e) {
JFileChooser jf = new JFileChooser();
jf.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
jf.showOpenDialog(null);
File file = jf.getSelectedFile();
if(file != null){
Task1.tf1.setText(file.getAbsolutePath());
}
}
}
class ChooseLocota implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JFileChooser JF = new JFileChooser();
JF.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
JF.showOpenDialog(null);
File file1 = JF.getSelectedFile();
if(file1 != null){
Task1.tf2.setText(file1.getAbsolutePath());
}
}
}
class CopyFile implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
String str1 = Task1.tf1.getText();
String str2 = Task1.tf2.getText();
File file1 = new File(str1);
File file2 = new File(str2);
if(file1.isFile()){
CopyFile(str1,str2);
}
if(file1.isDirectory() && file2.isDirectory()){
CopyDir(str1, str2);
}
}
public static boolean CopyFile(String srcPath , String destDir) {
boolean flag = false;
try {
File srcFile = new File(srcPath);
if(!srcFile.exists()){
JOptionPane.showMessageDialog(null, "文件不存在!");
}
File destFile = new File(destDir);
destFile.mkdirs();
if(!destFile.exists()){
JOptionPane.showMessageDialog(null, "文件目录不存在!");
}
String filename = srcPath.substring(srcPath.lastIndexOf(File.separator));
String destPath = destDir + filename;
File destFile1 = new File(destPath);
if(srcPath.equals(destPath)){
JOptionPane.showMessageDialog(null, "源路径和目标路径重复!");
return false;
}
if(destFile1.exists() && destFile1.isFile()){
JOptionPane.showMessageDialog(null, "目标路径下已有同名文件!");
return false;
}
FileInputStream in = new FileInputStream(srcPath);
FileOutputStream out = new FileOutputStream(destFile1);
byte[] buffer = new byte[1024];
int ins;
long start = System.currentTimeMillis();
while((ins = in.read(buffer)) != -1){
out.write(buffer, 0, ins);
}
out.flush();
in.close();
out.close();
long end = System.currentTimeMillis();
flag = true;
long time = end - start;
if(flag){
JOptionPane.showMessageDialog(null, "文件恢复成功!共恢复1份文件到"+destDir
+'\n'+"耗时"+time+"ms");
}
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
return flag;
}
public static boolean CopyDir(String srcPath , String destPath){
boolean flag = false;
File srcFile = new File(srcPath);
if(!srcFile.exists()){
JOptionPane.showMessageDialog(null, "文件夹不存在!");
}
String dirName = getDirName(srcPath);
String destDir = destPath + File.separator+dirName;
File[] fileList = srcFile.listFiles();
if(fileList.length == 0) {
flag = true;
}else {
//long start = System.currentTimeMillis();
for (File temp : fileList) {
if(temp.isFile()){
flag = CopyFile(temp.getAbsolutePath(),destDir);
}else if(temp.isDirectory()){
flag = CopyDir(temp.getAbsolutePath(), destDir);
}
//long end = System.currentTimeMillis();
//long time = end - start;
if(!flag) {
break;
}
}
}
if(flag){
JOptionPane.showMessageDialog(null, "恢复结束!"+"共恢复"+fileList.length+"份文件到"+destPath);
}
return flag;
}
private static String getDirName(String dir) {
if (dir.endsWith(File.separator)) { // 如果文件夹路径以"//"结尾,则先去除末尾的"//"
dir = dir.substring(0, dir.lastIndexOf(File.separator));
}
return dir.substring(dir.lastIndexOf(File.separator) + 1);
}
}
`