📄 dbcopy.java
字号:
package org.wuhang.transfer;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
public class DBCopy {
private File from_file;
private File to_file;
private JFileChooser chooser;
private final String from_DBPath = "Cjsjk-copy.jar";
private String to_DBPath = null;
public DBCopy(){
}
public boolean Choose_to_file(){
chooser = new JFileChooser();
chooser.setDialogTitle("选择数据库要保存的位置");
FileNameExtensionFilter filter = new FileNameExtensionFilter("Cjsjk DataBase","adb");
chooser.setFileFilter(filter);
Calendar date = Calendar.getInstance();
SimpleDateFormat o = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
chooser.setSelectedFile(new File("Cjsjk-db-"+o.format(date.getTime())+".adb"));
int returnVal = chooser.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
to_DBPath = chooser.getSelectedFile().getAbsolutePath();
to_DBPath = to_DBPath.replace('\\', '/');
return true;
}
else
return false;
}
public boolean Choose_file(){
chooser = new JFileChooser();
chooser.setDialogTitle("选择数据库要保存的位置");
FileNameExtensionFilter filter = new FileNameExtensionFilter("Cjsjk DataBase","adb");
chooser.setFileFilter(filter);
Calendar date = Calendar.getInstance();
SimpleDateFormat o = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
chooser.setSelectedFile(new File("Cjsjk-dbbackup-"+o.format(date.getTime())));
int returnVal = chooser.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
to_DBPath = chooser.getSelectedFile().getAbsolutePath();
to_DBPath = to_DBPath.replace('\\', '/');
return true;
}
else
return false;
}
public File getFrom_file() {
return from_file;
}
public void setFrom_file(File from_file) {
this.from_file = from_file;
}
public File getTo_file() {
return to_file;
}
public void setTo_file(File to_file) {
this.to_file = to_file;
}
public String CopyDB() {
try {
File temp_file = new File(from_DBPath);
setFrom_file(temp_file);
FileInputStream fis = new FileInputStream(getFrom_file());
if (Choose_to_file()) {
temp_file = new File(to_DBPath);
if (temp_file.exists()) {
JOptionPane.showMessageDialog(null, temp_file.getName()
+ "数据库已" + "存在!", "提示", JOptionPane.ERROR_MESSAGE);
return null;
} else {
setTo_file(temp_file);
FileOutputStream fos = new FileOutputStream(getTo_file());
byte[] b = new byte[1024];
int temp = 0;
while (true) {
temp = fis.read(b);
if (temp == -1)
break;
fos.write(b, 0, temp);
}
return to_DBPath;
}
}
else
return null;
} catch (Exception ee) {
ee.printStackTrace();
return null;
}
}
public boolean copyDirectory(String srcDirName){
if(Choose_file())
return copyDirectory(srcDirName,to_DBPath);
else
return false;
}
public boolean copyDirectory(String srcDirName,String destDirName){
return copyDirectory(srcDirName,destDirName,false);
}
public boolean copyDirectory(String srcDirName,String destDirName,boolean overlay){
File srcDir = new File(srcDirName);
if(!srcDir.exists()){
System.out.println("复制目录失败:源目录"+srcDirName+"不存在!");
return false;
}else if(!srcDir.isDirectory()){
System.out.println("复制目录失败:"+srcDirName+"不是一个目录!");
return false;
}
if(!destDirName.endsWith(File.separator)){
destDirName = destDirName + File.separator;
}
File destDir = new File(destDirName);
if(destDir.exists()){
if(overlay){
System.out.println("目标目录已存在,准备删除它!");
}
JOptionPane.showMessageDialog(null, "目标文件夹已存在!请重新取一个文件夹名",
"提示", JOptionPane.ERROR_MESSAGE);
return false;
}else{
System.out.println("目标目录不存在,准备创建它!");
if(!destDir.mkdir()){
System.out.println("复制失败,创建目标目录失败!");
return false;
}
}
boolean flag = true;
File[] files = srcDir.listFiles();
for(int i=0;i<files.length;i++){
if(files[i].isFile()){
flag = copyFile(files[i].getAbsolutePath(),destDirName + files[i].getName());
if(!flag)
break;
}
if(files[i].isDirectory()){
flag = copyDirectory(files[i].getAbsolutePath(),
destDirName + files[i].getName());
if(!flag)
break;
}
}
if(!flag){
System.out.println("复制目录失败!"+srcDirName+"至"+destDirName+"失败");
JOptionPane.showMessageDialog(null, "备份数据失败!", "提示", JOptionPane.ERROR_MESSAGE);
return false;
}
System.out.println("复制目录"+srcDirName + "至"+destDirName+"成功!");
return true;
}
public boolean copyFile(String srcFileName,String destFileName){
return copyFile(srcFileName,destFileName,false);
}
public boolean copyFile(String srcFileName,String destFileName,boolean overlay){
File srcFile = new File(srcFileName);
if(!srcFile.exists()){
System.out.println("源文件存在!");
return false;
}else if(!srcFile.isFile()){
System.out.println("不是个文件!");
return false;
}
File destFile = new File(destFileName);
if(destFile.exists()){
if(overlay){
}else{
System.out.println("目标文件已存在!");
return false;
}
}else{
if(!destFile.getParentFile().exists()){
if(!destFile.getParentFile().mkdirs()){
System.out.println("创建目标文件所在的目录失败!");
return false;
}
}
}
int byteread = 0;
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFile);
byte[] buffer = new byte[1024];
while((byteread = in.read(buffer))!=-1){
out.write(buffer,0,byteread);
}
return true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}finally{
if(out != null){
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
if(in != null)
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -