站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Java 专题一个自动备份程序

一个自动备份程序

摘要: 一个自动备份程序

</td> </tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width="100%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="198" height="86" align="center" valign="top"> </td> <td width="486" valign="top">
一、程序
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(); } }
</td> </tr> </table>
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

</td> </tr> <tr>


↑返回目录
前一篇: JAVA域对象持久化技术的比较
后一篇: 您的Java代码安全吗?