一、程序</td> </tr> </table>
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=C:/java/jar%jar,d:/java%java
用逗号隔开各需要备份的目录,用%号隔开要备份的目录与新的存放位置。
三、批处理文件
自动备份.bat
java BackupFile
↑返回目录
前一篇: JAVA域对象持久化技术的比较
后一篇: 您的Java代码安全吗?