Java實(shí)現(xiàn)定時(shí)備份文件
本文實(shí)例為大家分享了Java如何定時(shí)備份文件的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
程序思路:
1.空目錄不備份,但非空目錄都備份
2.源目錄 source 要遞歸他下面所有的文件和目錄 存入List
3.循環(huán)這個(gè)list,創(chuàng)建每個(gè)文件的目錄
4.開始復(fù)制
以下代碼實(shí)現(xiàn)了定時(shí)備份路徑為e:\\a的文件,每30秒進(jìn)行一次備份,時(shí)間可修改。
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("創(chuàng)建備份目錄?"+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(); //取出絕對(duì)路徑 e:\\a
String newpath = fpath.replace(SOURCE, DEST);
System.out.println("對(duì)應(yīng)的新路徑" + newpath);
File newFile = new File(newpath);
if (newFile.getParentFile().exists() == false) {
System.out.println("創(chuàng)建" + newFile + "的父目錄成功?" + newFile.getParentFile().mkdirs());
}
if (f.isFile()) {
copy(f, newFile);
}
}
}
//復(fù)制
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) {
//說(shuō)明有子目錄或子文件
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());
}
}
再為大家補(bǔ)充一段:
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();
}
}
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java開發(fā)SSM框架微信支付的實(shí)現(xiàn)
這篇文章主要介紹了Java開發(fā)SSM框架微信支付的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
Activiti如何動(dòng)態(tài)獲取流程圖過(guò)程詳解
這篇文章主要介紹了Activiti如何動(dòng)態(tài)獲取流程圖過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Java使用OTP動(dòng)態(tài)口令(每分鐘變一次)進(jìn)行登錄認(rèn)證
這篇文章主要介紹了Java使用OTP動(dòng)態(tài)口令(每分鐘變一次)進(jìn)行登錄認(rèn)證,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
SpringBoot獲取http數(shù)據(jù)、打印HTTP參數(shù)的4種方式
Java的話本地打斷點(diǎn)可以調(diào)試獲取rest入?yún)?但是在生產(chǎn)環(huán)境可能我們獲取入?yún)ⅲ℉ttp?header/parameter)可能就沒(méi)有那么的輕松了,所以本文給大家介紹了SpringBoot獲取http數(shù)據(jù)、打印HTTP參數(shù)的4種方式,需要的朋友可以參考下2024-03-03
Java實(shí)現(xiàn)mybatis批量插入數(shù)據(jù)到Oracle
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)mybatis批量插入數(shù)據(jù)到Oracle 的相關(guān)資料,需要的朋友可以參考下2016-06-06
優(yōu)雅地在Java應(yīng)用中實(shí)現(xiàn)全局枚舉處理的方法
這篇文章主要給大家介紹了關(guān)于如何優(yōu)雅地在Java應(yīng)用中實(shí)現(xiàn)全局枚舉處理的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
深入研究spring boot集成kafka之spring-kafka底層原理
這篇文章主要深入研究了spring boot集成kafka如何實(shí)現(xiàn)spring-kafka的底層原理分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2022-02-02

