Java实现定时备份⽂件本⽂实例为⼤家分享了Java如何定时备份⽂件的具体实现代码,供⼤家参考,具体内容如下程序思路:
1.空⽬录不备份,但⾮空⽬录都备份
2.源⽬录 source 要递归他下⾯所有的⽂件和⽬录存⼊List
3.循环这个list,创建每个⽂件的⽬录
4.开始复制
以下代码实现了定时备份路径为e:\\a的⽂件,每30秒进⾏⼀次备份,时间可修改。
public class Test12 {
public static void main(String[] args) throws InterruptedException {
Timer t = new Timer();
t.scheduleAtFixedRate(new MyTask(),new Date(),1000*30);
for(int i = 0;i<10000;i++){
Thread.sleep(1000);
System.out.println("warning");
}
}
}
class MyTask extends TimerTask{
static final String SOURCE = "e:\\a";
static String DEST;
@Override
public void run() {
Date d = new Date();
DateFormat df = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
DEST = "e:\\dest_" + df.format(d);
File file = new File(SOURCE);
File dest = new File(DEST);
ists()){
deleteAll(dest);
}
System.out.println("创建备份⽬录?"+dest.mkdirs());
List<File> list = new ArrayList<File>();
getAllFile(file,list);
backUp(list,dest);
}
//备份
private static void backUp(List<File> list, File dest) {
if (list == null || list.size() <= 0) {
return;
}
for (File f : list) {
String fpath = f.getAbsolutePath();    //取出绝对路径        e:\\a
String newpath = place(SOURCE, DEST);
System.out.println("对应的新路径" + newpath);
File newFile = new File(newpath);
if (ParentFile().exists() == false) {
System.out.println("创建" + newFile + "的⽗⽬录成功?" + ParentFile().mkdirs());
}
if (f.isFile()) {
copy(f, newFile);
}
}
}
//复制
private static void copy(File inFile, File outFile) {
FileInputStream fis = null;
FileOutputStream fos = null;
boolean isFlag = false;
try {
fis = new FileInputStream(inFile);
fos = new FileOutputStream(outFile);
byte[] bs = new byte[1024];
int length = -1;
while ((length = ad(bs)) != -1) {
fos.write(bs, 0, length);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//递归获取原⽬录下所有的⽂件信息
private static void getAllFile(File source, List<File> list) {        if (source.isDirectory()) {
java创建文件//查看⼦⽬录 listFile()
File[] fs = source.listFiles();
if (fs != null && fs.length > 0) {
//说明有⼦⽬录或⼦⽂件
for (File ff : fs) {
getAllFile(ff, list);
}
}
}
list.add(source);
}
//递归删除
private static void deleteAll(File f) {
if (f.isDirectory()) {
File[] fs = f.listFiles();
if (fs != null && fs.length > 0) {
for (File file : fs) {
deleteAll(file);
}
}
}
System.out.println(f + "删除成功?" + f.delete());
}
}
再为⼤家补充⼀段:
import java.util.Calendar;
import java.util.Date;
SimpleDateFormat;
import java.io.IOException;
import java.io.PrintStream;
public class Backup
{
public static void main(String[] args)
{
Runtime runtime = Runtime();
Calendar calendar = Instance();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentTime = dateFormat.Time());
Process p = null;
PrintStream print = null;
StringBuilder buf = new StringBuilder();
for(String a : args){
buf.append(a);
buf.append(" ");
}
String databases = String();
try{
p = ("cmd /c mysqldump -uroot -p1234 -B "+databases+">"+currentTime+".sql.bak");  }catch (IOException e){
if( p != null ){
p.destroy();
}
try{
print = new PrintStream(currentTime+"_backup_err.log");
dateFormat.applyPattern("yyyy-MM-dd HH:mm:ss");
currentTime = dateFormat.Time());
print.println(currentTime+"  backup failed.");
e.printStackTrace(print);
print.flush();
}catch (IOException e2){
}finally{
if(print!=null){
print.close();
}
}
}
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。