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.開(kāi)始復(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)文章
SpringBoot?實(shí)現(xiàn)微信推送模板的示例代碼
這篇文章主要介紹了SpringBoot?實(shí)現(xiàn)微信推送模板,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-12-12
Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法
這篇文章主要介紹了Java獲取一維數(shù)組的最小值實(shí)現(xiàn)方法,需要的朋友可以參考下2014-02-02
Java中try-catch-finally執(zhí)行順序你知道嗎
本文主要介紹了try-catch-finally執(zhí)行順序你知道嗎,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
教你使用springSecurity+jwt實(shí)現(xiàn)互踢功能
JWT作為一個(gè)開(kāi)放的標(biāo)準(zhǔn)( RFC 7519 ),定義了一種簡(jiǎn)潔的,自包含的方法用于通信雙方之間以Json對(duì)象的形式安全的傳遞信息。接下來(lái)通過(guò)本文給大家介紹springSecurity+jwt實(shí)現(xiàn)互踢功能,需要的朋友可以參考下2021-11-11
SpringBoot自定義全局異常處理器的問(wèn)題總結(jié)
Springboot框架提供兩個(gè)注解幫助我們十分方便實(shí)現(xiàn)全局異常處理器以及自定義異常,處理器會(huì)優(yōu)先處理更具體的異常類(lèi)型,如果沒(méi)有找到匹配的處理器,那么它會(huì)尋找處理更一般異常類(lèi)型的處理器,本文介紹SpringBoot自定義全局異常處理器的問(wèn)題,一起看看吧2024-01-01
java實(shí)現(xiàn)動(dòng)態(tài)代理方法淺析
這篇文章主要介紹了java實(shí)現(xiàn)動(dòng)態(tài)代理方法淺析,很實(shí)用的功能,需要的朋友可以參考下2014-08-08
深入了解Spring中最常用的11個(gè)擴(kuò)展點(diǎn)
我們一說(shuō)到spring,可能第一個(gè)想到的是?IOC(控制反轉(zhuǎn))?和?AOP(面向切面編程)。除此之外,我們?cè)谑褂胹pring的過(guò)程中,有沒(méi)有發(fā)現(xiàn)它的擴(kuò)展能力非常強(qiáng)。今天就來(lái)跟大家一起聊聊,在Spring中最常用的11個(gè)擴(kuò)展點(diǎn)2022-09-09

