服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java实现定时备份文件

Java实现定时备份文件

2021-11-30 11:13HeyHex Java教程

这篇文章主要为大家详细介绍了Java实现定时备份文件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Java如何定时备份文件的具体实现代码,供大家参考,具体内容如下

程序思路:

1.空目录不备份,但非空目录都备份

2.源目录 source 要递归他下面所有的文件和目录 存入List

3.循环这个list,创建每个文件的目录

4.开始复制

以下代码实现了定时备份路径为e:\\a的文件,每30秒进行一次备份,时间可修改。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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);
 
        if(dest.exists()){
            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 = fpath.replace(SOURCE, DEST);
            System.out.println("对应的新路径" + newpath);
            File newFile = new File(newpath);
            if (newFile.getParentFile().exists() == false) {
                System.out.println("创建" + newFile + "的父目录成功?" + newFile.getParentFile().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 = fis.read(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()) {
            //查看子目录 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());
    }
 
}

再为大家补充一段:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.IOException;
import java.io.PrintStream;
 
public class Backup
{
 public static void main(String[] args)
 {
  Runtime runtime = Runtime.getRuntime();
  Calendar calendar = Calendar.getInstance();
  SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HHmmss");
  String currentTime = dateFormat.format(calendar.getTime());
  Process p = null;
  PrintStream print = null;
  StringBuilder buf = new StringBuilder();
  for(String a : args){
   buf.append(a);
   buf.append(" ");
  }
  String databases = buf.toString();
  
  try{
   p = runtime.exec("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.format(calendar.getTime());
    print.println(currentTime+"  backup failed.");
    e.printStackTrace(print);
    print.flush();
   }catch (IOException e2){
 
   }finally{
    if(print!=null){
     print.close();
    }
   }
  }
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_45213010/article/details/111992387

延伸 · 阅读

精彩推荐