Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 334|回复: 0

[JavaIO学习]java同步两目录的实用程序

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-11-3 23:58:30 | 显示全部楼层 |阅读模式
    java同步两目录的实用程序,自已备份目录就用这个.将下面文件放在c:        est下编译,在桌面上放一个Sndir.bat,双击就行了. 这是Sndir.bat的内容:
    cd
    cd test
    java CopyDirectory
    pause

    1. import java.io.File;
    2. import java.io.FileInputStream;
    3. import java.io.FileOutputStream;
    4. import java.io.IOException;
    5. import java.util.Vector;
    6. /*******************************************************************************
    7. * 功能说明: 复制指定文件夹内容到另一指定文件夹中
    8. * @author qianshu
    9. * @time 2012-6-19
    10. ******************************************************************************/
    11. public class CopyDirectory {
    12.        
    13.         static String strPath = "c:/java";  // 源目录,工作目录。                 
    14.         static String strPath2 = "E:/to";  // 目标目录,备份目录。
    15. /**
    16. * 功能说明: 将strPath目录下的文件复制到strPath2目录下
    17. *
    18. * @param strPath
    19. * @param strPath2
    20. */
    21.         public void copy(String strPath,String strPath2 ){
    22.                 if (!new File(strPath2).exists()) // 目标目录不存在时则创建目录。
    23.                 {                         
    24.                         if(new File(strPath2).mkdir()){                                 
    25.                                 System.out.println("文件夹 "+strPath2+" 创建成功!");
    26.                         }else{         
    27.                System.out.println("文件夹 "+strPath2+" 创建失败!请检查目标路径是否可用!");                                 
    28.                System.exit(1);//非正常退出                         
    29.                }                 
    30.                 }  
    31.                 Vector < File> vf = new Vector < File>();  //将多个元素存在一个集合里               
    32.                 getDir(strPath, vf); //得到当前运行的文件的绝对目录
    33.                                
    34.                       for (int i = 0; i  < vf.size(); i++) {
    35.                         File f=vf.get(i);
    36.                         String ss = f.toString(); //源文件的文件路径
    37.                         String ss1 = (strPath2 + ss.substring(strPath.length())); //目标文件的文件路径
    38.                         File f2 = new File(ss1);   
    39.                         if (!comparef(f, f2)) {
    40.                                 copys(f, f2);  
    41.             
    42.                                 } else {                 
    43.                                         System.out.println("两个文件相同!");                 
    44.                       }  
    45.                          }                 
    46.                 System.out.println("同步已完成!");         
    47.         }
    48.         // 递归
    49.     public long getFileSize(File f)//取得文件夹大小
    50.     {
    51.         long size = 0;
    52.         File flist[] = f.listFiles();
    53.         for (int i = 0; i < flist.length; i++)
    54.         {
    55.             if (flist[i].isDirectory())
    56.             {
    57.                 size = size + getFileSize(flist[i]);
    58.             } else
    59.             {
    60.                 size = size + flist[i].length();
    61.             }
    62.         }
    63.         return size;
    64.     }
    65.    
    66.        public long getDirSize(File f){//递归求取目录中文件(包括目录)个数
    67.         long size = 0;
    68.         File flist[] = f.listFiles();
    69.         size=flist.length;
    70.         for (int i = 0; i < flist.length; i++) {
    71.             if (flist[i].isDirectory()) {
    72.                 size = size + getDirSize(flist[i]);
    73.                
    74.             }
    75.         }
    76.         return size;
    77.     }
    78.        
    79.         /**
    80.          * 功能说明: 比较strPath下的当前f文件与strPath2下的对应f2文件是否相同
    81.          * 文件长度和最后修改时间都相同,两文件才相同
    82.          *
    83.          * @param f
    84.          * @param f2
    85.          * @return boolean 如返回true为两文件相同,返回false为两文件不同
    86.          */
    87.         public  boolean comparef(File f, File f2) {
    88.        if(f.isDirectory()&&f2.isDirectory()&&(getDirSize(f)!=getDirSize(f2)||getFileSize(f)!=getFileSize(f2)))
    89.                  return false;
    90.                
    91.               
    92.                 if (f.length() != f2.length() || f.lastModified() != f2.lastModified()) {
    93.                         return false;
    94.                 } else {
    95.                         return true;
    96.                 }
    97.         }  
    98.         /**
    99.          * 功能说明: 将strPath目录下的当前f文件复制到strPath2相应目录下
    100.          * 如果当前f文件是目录就重新得到当前目录下的子目录,如果是文件就直接复制
    101.          * @param f   
    102.          * @param f2
    103.          */
    104.         public void copys(File f, File f2) {
    105.                 try {
    106.                         if (f.isDirectory()) {
    107.                                 f2.mkdirs();
    108.                                 f2.setLastModified(f.lastModified());
    109.                                 // 修改最后修改时间属性。
    110.                                 String strPath = f.getPath().toString();
    111.                                 String strPath2 = f2.getPath().toString();
    112.                                 this.copy(strPath, strPath2);
    113.                         }
    114.                         if (f.isFile()) {
    115.                                         FileInputStream fis = new FileInputStream(f);
    116.                                         FileOutputStream fos = new FileOutputStream(f2);
    117.                                         byte[] b = new byte[10240];
    118.                                         int s = fis.read(b);
    119.                                         while (s != -1) {
    120.                                                 fos.write(b, 0, s);
    121.                                                 s = fis.read(b);
    122.                                         }
    123.                                         fis.close();
    124.                                         fos.close();
    125.                                         System.out.println("文件 " + f2 + " 更新成功!");
    126.                                         f2.setLastModified(f.lastModified());// 修改最后修改时间属性。
    127.                         }
    128.                 } catch (IOException e) {
    129.                         e.printStackTrace();
    130.                         System.out.println("文件创建失败!可能是磁盘已满或目标文件拒绝访问!");
    131.                 } catch (SecurityException e) {
    132.                         e.printStackTrace();
    133.                         System.out.println("文件创建失败,可能是因为没有足够的权限!");
    134.                 }
    135.         }  
    136.        
    137.         /***************************************************************************
    138.          * 功能说明: 获得当前文件夹中的文件列表
    139.          *
    140.          * @param strPath 当前文件夹的绝对路径 如:e:/from
    141.          * @param vf      保存文件目录列表的数组
    142.          * @author qianshu
    143.          **************************************************************************/
    144.         public void getDir(String strPath, Vector< File> vf) {
    145.                 try {
    146.                         File f = new File(strPath);
    147.                         File[] fList = f.listFiles();
    148.                         for (int j = 0; j < fList.length; j++) {               
    149.                                 vf.add(fList[j]);                                       
    150.                         }
    151.                 } catch (Exception e) {
    152.                         System.out.println("获取源目录列表失败,请检查源目录路径是否正确!");
    153.                 }
    154.         }
    155.         /**
    156.          * 功能说明:每隔30秒进行一次复制操作
    157.          *
    158.          * @param args
    159.          */
    160.         public static void main(String[] args) {
    161.                 while (true) {
    162.                         try {
    163.                                 new Thread(new Runnable() {
    164.                                         public synchronized void run() {
    165.                                 CopyDirectory cd = new CopyDirectory();
    166.                                 cd.copy(strPath, strPath2);
    167.                                 System.out.println("同步已完成!");
    168.                                         }
    169.                                 }).start();
    170.                         } catch (Exception e) {
    171.                                 System.out.println("读取异常");
    172.                         }
    173.                         try {
    174.                                 Thread.sleep(30 * 1000);
    175.                         } catch (Exception e) {
    176.                         }
    177.                 }
    178.         }
    179. }
    复制代码


       
         
         
          
          

            
          

            
          
         
       

      


    源码下载:http://file.javaxxz.com/2014/11/3/235830640.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-25 14:09 , Processed in 0.295893 second(s), 36 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表