TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
java同步两目录的实用程序,自已备份目录就用这个.将下面文件放在c: est下编译,在桌面上放一个Sndir.bat,双击就行了. 这是Sndir.bat的内容:
cd
cd test
java CopyDirectory
pause
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Vector;
- /*******************************************************************************
- * 功能说明: 复制指定文件夹内容到另一指定文件夹中
- * @author qianshu
- * @time 2012-6-19
- ******************************************************************************/
- public class CopyDirectory {
-
- static String strPath = "c:/java"; // 源目录,工作目录。
- static String strPath2 = "E:/to"; // 目标目录,备份目录。
- /**
- * 功能说明: 将strPath目录下的文件复制到strPath2目录下
- *
- * @param strPath
- * @param strPath2
- */
- public void copy(String strPath,String strPath2 ){
- if (!new File(strPath2).exists()) // 目标目录不存在时则创建目录。
- {
- if(new File(strPath2).mkdir()){
- System.out.println("文件夹 "+strPath2+" 创建成功!");
- }else{
- System.out.println("文件夹 "+strPath2+" 创建失败!请检查目标路径是否可用!");
- System.exit(1);//非正常退出
- }
- }
- Vector < File> vf = new Vector < File>(); //将多个元素存在一个集合里
- getDir(strPath, vf); //得到当前运行的文件的绝对目录
-
- for (int i = 0; i < vf.size(); i++) {
- File f=vf.get(i);
- String ss = f.toString(); //源文件的文件路径
- String ss1 = (strPath2 + ss.substring(strPath.length())); //目标文件的文件路径
- File f2 = new File(ss1);
- if (!comparef(f, f2)) {
- copys(f, f2);
-
- } else {
- System.out.println("两个文件相同!");
- }
- }
- System.out.println("同步已完成!");
- }
- // 递归
- public long getFileSize(File f)//取得文件夹大小
- {
- long size = 0;
- File flist[] = f.listFiles();
- for (int i = 0; i < flist.length; i++)
- {
- if (flist[i].isDirectory())
- {
- size = size + getFileSize(flist[i]);
- } else
- {
- size = size + flist[i].length();
- }
- }
- return size;
- }
-
- public long getDirSize(File f){//递归求取目录中文件(包括目录)个数
- long size = 0;
- File flist[] = f.listFiles();
- size=flist.length;
- for (int i = 0; i < flist.length; i++) {
- if (flist[i].isDirectory()) {
- size = size + getDirSize(flist[i]);
-
- }
- }
- return size;
- }
-
- /**
- * 功能说明: 比较strPath下的当前f文件与strPath2下的对应f2文件是否相同
- * 文件长度和最后修改时间都相同,两文件才相同
- *
- * @param f
- * @param f2
- * @return boolean 如返回true为两文件相同,返回false为两文件不同
- */
- public boolean comparef(File f, File f2) {
- if(f.isDirectory()&&f2.isDirectory()&&(getDirSize(f)!=getDirSize(f2)||getFileSize(f)!=getFileSize(f2)))
- return false;
-
-
- if (f.length() != f2.length() || f.lastModified() != f2.lastModified()) {
- return false;
- } else {
- return true;
- }
- }
- /**
- * 功能说明: 将strPath目录下的当前f文件复制到strPath2相应目录下
- * 如果当前f文件是目录就重新得到当前目录下的子目录,如果是文件就直接复制
- * @param f
- * @param f2
- */
- public void copys(File f, File f2) {
- try {
- if (f.isDirectory()) {
- f2.mkdirs();
- f2.setLastModified(f.lastModified());
- // 修改最后修改时间属性。
- String strPath = f.getPath().toString();
- String strPath2 = f2.getPath().toString();
- this.copy(strPath, strPath2);
- }
- if (f.isFile()) {
- FileInputStream fis = new FileInputStream(f);
- FileOutputStream fos = new FileOutputStream(f2);
- byte[] b = new byte[10240];
- int s = fis.read(b);
- while (s != -1) {
- fos.write(b, 0, s);
- s = fis.read(b);
- }
- fis.close();
- fos.close();
- System.out.println("文件 " + f2 + " 更新成功!");
- f2.setLastModified(f.lastModified());// 修改最后修改时间属性。
- }
- } catch (IOException e) {
- e.printStackTrace();
- System.out.println("文件创建失败!可能是磁盘已满或目标文件拒绝访问!");
- } catch (SecurityException e) {
- e.printStackTrace();
- System.out.println("文件创建失败,可能是因为没有足够的权限!");
- }
- }
-
- /***************************************************************************
- * 功能说明: 获得当前文件夹中的文件列表
- *
- * @param strPath 当前文件夹的绝对路径 如:e:/from
- * @param vf 保存文件目录列表的数组
- * @author qianshu
- **************************************************************************/
- public void getDir(String strPath, Vector< File> vf) {
- try {
- File f = new File(strPath);
- File[] fList = f.listFiles();
- for (int j = 0; j < fList.length; j++) {
- vf.add(fList[j]);
- }
- } catch (Exception e) {
- System.out.println("获取源目录列表失败,请检查源目录路径是否正确!");
- }
- }
- /**
- * 功能说明:每隔30秒进行一次复制操作
- *
- * @param args
- */
- public static void main(String[] args) {
- while (true) {
- try {
- new Thread(new Runnable() {
- public synchronized void run() {
- CopyDirectory cd = new CopyDirectory();
- cd.copy(strPath, strPath2);
- System.out.println("同步已完成!");
- }
- }).start();
- } catch (Exception e) {
- System.out.println("读取异常");
- }
- try {
- Thread.sleep(30 * 1000);
- } catch (Exception e) {
- }
- }
- }
- }
复制代码
源码下载:http://file.javaxxz.com/2014/11/3/235830640.zip |
|