lillian1205

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            0 Posts :: 5 Stories :: 0 Comments :: 0 Trackbacks
          需要做一個簡單的定期數據備份,開始想到是 quatz ,查了些資料發現 quatz 功能比較強大,而自己的需求很簡單,能定期備份數據就行。
          查到了util.timer何timertask兩個類,可以定期執行任務。而數據備份做一個讀取文件和文件夾的就可以。如下:
           
          1.數據備份
           1package copy;
           2
           3import java.io.*;
           4
           5public class CopyDirectory {
           6    // 源文件夾
           7    static String url1 = "test";
           8   //  目標文件夾
           9    static String url2 = "F:/mirc1/";
          10    
          11    
          12    public static void copy ()throws IOException{
          13
          14        (new File(url2)).mkdirs();
          15        File[] file = (new File(url1)).listFiles();
          16        for (int i = 0; i < file.length; i++{
          17            if (file[i].isFile()) {
          18                copyFile(file[i], new File(url2 + file[i].getName()));
          19            }

          20            if (file[i].isDirectory()) {
          21                String sourceDir = url1 + File.separator + file[i].getName();
          22                String targetDir = url2 + File.separator + file[i].getName();
          23                copyDirectiory(sourceDir, targetDir);
          24            }

          25        }

          26    }

          27
          28    public static void copyFile(File sourceFile, File targetFile)
          29            throws IOException {
          30        FileInputStream input = new FileInputStream(sourceFile);
          31        BufferedInputStream inBuff = new BufferedInputStream(input);
          32
          33        FileOutputStream output = new FileOutputStream(targetFile);
          34        BufferedOutputStream outBuff = new BufferedOutputStream(output);
          35
          36        byte[] b = new byte[1024 * 5];
          37        int len;
          38        while ((len = inBuff.read(b)) != -1{
          39            outBuff.write(b, 0, len);
          40        }

          41        outBuff.flush();
          42
          43        inBuff.close();
          44        outBuff.close();
          45        output.close();
          46        input.close();
          47    }

          48
          49    public static void copyDirectiory(String sourceDir, String targetDir)
          50            throws IOException {
          51        (new File(targetDir)).mkdirs();
          52        File[] file = (new File(sourceDir)).listFiles();
          53        for (int i = 0; i < file.length; i++{
          54            if (file[i].isFile()) {
          55                File sourceFile = file[i];
          56                File targetFile = new File(new File(targetDir)
          57                        .getAbsolutePath()
          58                        + File.separator + file[i].getName());
          59                copyFile(sourceFile, targetFile);
          60            }

          61            if (file[i].isDirectory()) {
          62                String dir1 = sourceDir + "/" + file[i].getName();
          63                String dir2 = targetDir + "/" + file[i].getName();
          64                copyDirectiory(dir1, dir2);
          65            }

          66        }

          67    }

          68}

          69

          2 . 設定數據備份工作執行時間間隔
           1package copy;
           2
           3import java.util.Date;
           4import java.util.Timer;
           5
           6public class CopyWork {
           7    public static void main(String[] args) {
           8        Date date = new Date();
           9        Timer timer = new Timer();
          10        timer.schedule(new Worker(), new Date(), 60000);
          11    }

          12  
          13}

          14

          3.測試
           1package copy;
           2
           3import java.io.IOException;
           4import java.util.TimerTask;
           5
           6public class Worker extends TimerTask{
           7
           8    @Override
           9    public void run() {
          10        try {
          11            CopyDirectory.copy();
          12        }
           catch (IOException e) {
          13            
          14            e.printStackTrace();
          15        }

          16        
          17    }

          18
          19}

          20
          posted on 2009-09-29 15:40 lillian 閱讀(478) 評論(0)  編輯  收藏 所屬分類: 數據備份

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 北辰区| 阜阳市| 平舆县| 灵丘县| 泗阳县| 海阳市| 虹口区| 高邑县| 惠水县| 河西区| 天等县| 乌海市| 桂平市| 平安县| 沽源县| 明光市| 胶南市| 长顺县| 布拖县| 乐山市| 兰州市| 洛川县| 堆龙德庆县| 桐庐县| 苍溪县| 阿拉善右旗| 中卫市| 大兴区| 城口县| 白城市| 福海县| 铜川市| 桦南县| 盐津县| 安吉县| 阳春市| 长治市| 昌乐县| 平昌县| 金塔县| 太谷县|