TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- 一、程序
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.util.Date;
- import java.text.SimpleDateFormat;
- import java.util.Properties;
- public class BackupFile {//将oldPath目录下的所有文件复制到newPath
- public void copyFolder(String oldPath, String newPath) {
- try{
- (new File(newPath)).mkdirs();
- File a = new File(oldPath);
- String f[] = a.list();
- File temp = null;
- for (int i=0;i< f.length; i++) {
- if(oldPath.endsWith(File.separator)){
- temp = new File(oldPath + f[i]);
- }
- else{
- temp = new File(oldPath + File.separator + f[i]);
- }
-
- if(temp.isFile()){
- FileInputStream input = new FileInputStream(temp);
- FileOutputStream output =
- new FileOutputStream(newPath + "/" + (temp.getName()).toString());
- byte[] b = new byte[1024*5];
- int len;
- while((len=input.read(b))!= -1){
- output.write(b,0,len);
- }
- output.flush();
- output.close();
- input.close();
- }
- if(temp.isDirectory()){
- copyFolder(oldPath + "/" + f[i], newPath + "/" +f[i]);
- }
- }
- }
- catch(Exception e){
- e.printStackTrace();
- }
- }
-
-
复制代码
- public String getPro(String name,String path) {
- try{
- Properties pro = new Properties();
- FileInputStream fin = new FileInputStream(path);
- pro.load(fin);
- return pro.getProperty(name);
- }
- catch(Exception e){
- e.printStackTrace();
- return null;
- }
- }
-
- public static void main(String args[]){
- BackupFile bf = new BackupFile();
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
- File f=new File("c:/java/网站备份专用/网站备份日期-"+sdf.format(new Date()));
- f.mkdirs();
- String folder = bf.getPro("BackupFolders","c:/java/BackupFolders.properties");
- System.out.println("取得所有的目录"+folder);
- if(folder!=null){
-
- String folders [] = folder.split(",");
- for(int i=0; i< folders.length; i++ ){
- String roots[] = folders[i].split("%");
- bf.copyFolder(roots[0],"c:/java/网站备份专用/"+f.getName()+"/"+roots[1]);
- }
-
- }
- }
- }
复制代码
二、用属性文件BackupFolders.properties存放要备份的目录,格式如下: BackupFolders=C:/java/jar%jar,d:/java%java 用逗号隔开各需要备份的目录,用%号隔开要备份的目录与新的存放位置。 三、批处理文件
自动备份.bat java BackupFile
源码下载:http://file.javaxxz.com/2014/10/2/001029453.zip |
|