2个回答
2011-04-02
展开全部
,为何不用平台无关的方式去处理文件复制呢(虽然输入输出流代码多了几行),还有renameTo方法用的时候小心,如果sourcePath与destPath所在分区类型不一致时可能会有问题.
我测试过了,没有问题,问题可能出在您的delAllFile方法中
package databackup;
import java.io.File;
import java.io.IOException;
public class DataBackup {
/**
* @param args
*/
public static void main(String[] args) {
String sourcePath = "D:\\Temp\\";
try {
// 源路径
String strDataBaseFilePath = sourcePath + "data\\CYSystem.mdb";
// dest路径
String strBackupDataBaseFilePath = sourcePath + "save\\";
delAllFile(strBackupDataBaseFilePath);
// 如果没有正常清空 备份文件夹的话,有可能重命名不成功.
try {
Process process= Runtime.getRuntime().exec(
"cmd.exe /c start /b copy " + strDataBaseFilePath + " "
+ strBackupDataBaseFilePath);
process.waitFor();
} catch (Exception e) {
System.out.println(e);
}
// create filename
java.util.Calendar now = java.util.Calendar.getInstance();
java.text.SimpleDateFormat yearAndMonth = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
String retString = yearAndMonth.format(now.getTime());
File file = new File(strBackupDataBaseFilePath + "CYSystem.mdb");
file.renameTo(new File(strBackupDataBaseFilePath + retString
+ ".mdb"));
} catch (Exception ex) {
ex.getStackTrace();
}
System.out.println("备份成功!");
}
public static void delAllFile(String path) {
try {
Process process=Runtime.getRuntime().exec(
"cmd.exe /c start /b del " + path +" "+"/q");
process.waitFor();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
另外,虚机团上产品团购,超级便宜
我测试过了,没有问题,问题可能出在您的delAllFile方法中
package databackup;
import java.io.File;
import java.io.IOException;
public class DataBackup {
/**
* @param args
*/
public static void main(String[] args) {
String sourcePath = "D:\\Temp\\";
try {
// 源路径
String strDataBaseFilePath = sourcePath + "data\\CYSystem.mdb";
// dest路径
String strBackupDataBaseFilePath = sourcePath + "save\\";
delAllFile(strBackupDataBaseFilePath);
// 如果没有正常清空 备份文件夹的话,有可能重命名不成功.
try {
Process process= Runtime.getRuntime().exec(
"cmd.exe /c start /b copy " + strDataBaseFilePath + " "
+ strBackupDataBaseFilePath);
process.waitFor();
} catch (Exception e) {
System.out.println(e);
}
// create filename
java.util.Calendar now = java.util.Calendar.getInstance();
java.text.SimpleDateFormat yearAndMonth = new java.text.SimpleDateFormat(
"yyyy-MM-dd");
String retString = yearAndMonth.format(now.getTime());
File file = new File(strBackupDataBaseFilePath + "CYSystem.mdb");
file.renameTo(new File(strBackupDataBaseFilePath + retString
+ ".mdb"));
} catch (Exception ex) {
ex.getStackTrace();
}
System.out.println("备份成功!");
}
public static void delAllFile(String path) {
try {
Process process=Runtime.getRuntime().exec(
"cmd.exe /c start /b del " + path +" "+"/q");
process.waitFor();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
}
另外,虚机团上产品团购,超级便宜
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询