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入门到精通教程
查看: 286|回复: 0

[Java线程学习]多线程自动监视并扫描指定文件夹中的文件变化

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

    [LV.1]初来乍到

    发表于 2014-11-4 23:59:54 | 显示全部楼层 |阅读模式
    采用多线程自动监视并扫描指定文件夹中的文件变化(文件数的变化和修改日期的变化),,实现其功能的代码由四个java文件组成 :FileListener.java,FileMonitor.java,FileTableModel.java,ParseUtility.java.其具体代码如下: FileListener.java /*
      * FileListener.java
      *
      * Created on 2007-9-27, 17:58:49
      *
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
      
       
       
         
       

         
       
      
         

    package DirectoryScanner;
    /**
      *
      * @author Hale Chou
      */
    public interface FileListener {
         
         void dirChanged(FileMonitor monitor);
         
    }
    FileMonitor.java
    /*
      * FileMonitor.java
      *
      * Created on 2007-9-27, 17:59:05
      *
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
    package DirectoryScanner;
    import java.io.File;
    import java.util.ArrayList;
    import java.util.List;  
    /**
      *
      * @author Hale Chou
      */
    public class FileMonitor implements Runnable {
         List<FileListener> listenerList = new ArrayList<FileListener>();
         
         private File dir;
         private long interval;
         private long lastScanedTime;
         private int oldLength;
       
         public void addListener(FileListener listener) {
             listenerList.add(listener);
         }
       
         public void start(boolean isDamon) {
             Thread t = new Thread(this);
             t.setDaemon(isDamon);
             t.start();
         }
         public FileMonitor(String _dir, long intervalTime) {
             File f = new File(_dir);        
             if(!f.isDirectory())
                 throw new RuntimeException();
             
             //筛选出不是标准文件的目录
             File[] fileList = f.listFiles();
             for( int i = 0; i < fileList.length; i++ ) {
                 if(fileList.isDirectory()) {
                     fileList.delete();
                 }
             }
                
             this.dir = f;
             this.interval = intervalTime ;
             this.oldLength = dir.listFiles().length;
         }
       
         public File[] getMonitoredFile() {
             return this.dir.listFiles();
         }
       
         public void run() {
             while(true) {
                 if(!dir.isDirectory())
                     throw new RuntimeException();
                 if(filesChanged()) {
                     fireFilesChangedEvent(this);
                 }
                 try {
                     Thread.sleep(interval);
                 }catch(InterruptedException e) {
                     e.printStackTrace();
                 }
             }
         }
       
         private void fireFilesChangedEvent(FileMonitor monitor) {
             for( int i = 0; i < listenerList.size(); i ++ ) {
                 listenerList.get(i).dirChanged(monitor);
             }
         }
       
         protected boolean filesChanged() {
             File[] newFiles = dir.listFiles();
             if(newFiles.length != oldLength) {
                 oldLength = newFiles.length;
                 //System.out.println(newFiles.length);
                 return true;
             }
             for( int i = 0; i < newFiles.length; i++ ) {
                 if(newFiles.lastModified() >= lastScanedTime) {
                     return true;
                 }
             }
             return false ;
         }
    }

    FileTableModel.java
    /*
      * FileTableModel.java
      *
      * Created on 2007-9-27, 17:59:28
      *
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
    package DirectoryScanner;
    import java.io.File;
    import javax.swing.table.AbstractTableModel;
    /**
      *
      * @author Hale Chou
      */
    public class FileTableModel extends AbstractTableModel implements FileListener {
         private File[] files = null ;
         
         public FileTableModel(File dir) {
             files = dir.listFiles();        
         }
         public FileTableModel(File[] ff) {
             this.files = ff;            
         }
         
         public int getRowCount() {
             return  files.length;
         }
       
         //  Get a column"s name.
         @Override
         public String getColumnName(int col) {
             String s = "文件名,路径,大小,修改时间";
             return s.split(",")[col];
         }
         
         public int getColumnCount() {
             return 4;
         }
       
         public Object getValueAt(int rowIndex, int columnIndex) {
             File f = files[rowIndex];
             
             if(f.isDirectory()){
                 f.delete();
             }else{
                 switch(columnIndex){
                 case 0 :
                     return f.getName();
                 case 1 :
                 return f.getPath();
                 case 2 :
                 return ParseUtility.bytes2kb(f.length());
                 case 3 :
                     return ParseUtility.toYYYYMMDDHHMMSS(f.lastModified());
                 }
             }
             
             return "";
         }
       
         public void dirChanged(FileMonitor monitor) {
             this.files = monitor.getMonitoredFile();
             fireTableDataChanged();
         }
             
    }

    ParseUtility.java
    /*
      * ParseUtility.java
      *
      * Created on 2007-9-27, 17:59:55
      *
      * To change this template, choose Tools | Templates
      * and open the template in the editor.
      */
    package DirectoryScanner;
    import java.math.BigDecimal;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    /**
      *
      * @author Administrator
      */
    public class ParseUtility {
         
         public ParseUtility(){
         }
         
         public static String toYYYYMMDDHHMMSS(long time) {
             SimpleDateFormat format = new SimpleDateFormat("M月d日H时m分s秒");
             return format.format(new Date(time));
         }
         
         public static String bytes2kb(long bytes) {
             BigDecimal filesize = new BigDecimal(bytes);
             BigDecimal megabyte = new BigDecimal(1024 * 1024);
             float returnValue = filesize.divide(megabyte, 2 , BigDecimal.ROUND_UP).floatValue();
             if (returnValue > 1)
                 return(returnValue + " MB");
             BigDecimal kilobyte = new BigDecimal(1024);
             returnValue = filesize.divide(kilobyte, 2 , BigDecimal.ROUND_UP).floatValue();
             return (returnValue + " KB");
         }
       
    }

    最后是调用程序:
    /*
      * FileMonitorTest.java
      *
      * Created on 2007年9月28日, 上午9:21
      */
    package file;
    import java.io.File;
    import javax.swing.table.TableModel;
    import javax.swing.JFileChooser;
    import javax.swing.event.*;
    import DirectoryScanner.*;
    /**
      *
      * @author  Hale Chou
      */
    public class FileMonitorTest extends javax.swing.JFrame {
         
         public TableModel tableModel;
         
         /** Creates new form FileMonitorTest */
         public FileMonitorTest() {                     
             initComponents();            
         }
         
         /** This method is called from within the constructor to
          * initialize the form.
          * WARNING: Do NOT modify this code. The content of this method is
          * always regenerated by the Form Editor.
          */
         // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
         private void initComponents() {
             jScrollPane1 = new javax.swing.JScrollPane();
             jTable1 = new javax.swing.JTable();
             jLabel1 = new javax.swing.JLabel();
             txt_FilesCount = new javax.swing.JTextField();
             jLabel2 = new javax.swing.JLabel();
             txt_ScannedDirectory = new javax.swing.JTextField();
             btn_StartScan = new javax.swing.JButton();
             btn_SelectDirectory = new javax.swing.JButton();
             setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
             jTable1.setModel(new javax.swing.table.DefaultTableModel(
                 new Object [][] {
                 },
                 new String [] {
                 }
             ));
             jScrollPane1.setViewportView(jTable1);
             jLabel1.setText("当前文件数:");
             txt_FilesCount.setEditable(false);
             jLabel2.setText("选择文件夹:");
             btn_StartScan.setText("开始监视");
             btn_StartScan.addActionListener(new java.awt.event.ActionListener() {
                 public void actionPerformed(java.awt.event.ActionEvent evt) {
                     btn_StartScanActionPerformed(evt);
                 }
             });
             btn_SelectDirectory.setText("选择目录");
             btn_SelectDirectory.addActionListener(new java.awt.event.ActionListener() {
                 public void actionPerformed(java.awt.event.ActionEvent evt) {
                     btn_SelectDirectoryActionPerformed(evt);
                 }
             });
             javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
             getContentPane().setLayout(layout);
             layout.setHorizontalGroup(
                 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
                 .addGroup(layout.createSequentialGroup()
                     .addContainerGap()
                     .addComponent(jLabel2)
                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                     .addComponent(txt_ScannedDirectory, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                     .addComponent(btn_SelectDirectory)
                     .addContainerGap(31, Short.MAX_VALUE))
                 .addGroup(layout.createSequentialGroup()
                     .addContainerGap()
                     .addComponent(jLabel1)
                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                     .addComponent(txt_FilesCount, javax.swing.GroupLayout.PREFERRED_SIZE, 198,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                     .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                     .addComponent(btn_StartScan)
                     .addContainerGap(31, Short.MAX_VALUE))
             );
             layout.setVerticalGroup(
                 layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                 .addGroup(layout.createSequentialGroup()
                     .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 170,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                     .addGap(18, 18, 18)
                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                         .addComponent(jLabel2)
                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                             .addComponent(txt_ScannedDirectory, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                             .addComponent(btn_SelectDirectory)))
                     .addGap(18, 18, 18)
                     .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                         .addComponent(jLabel1)
                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                             .addComponent(txt_FilesCount, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                             .addComponent(btn_StartScan)))
                     .addContainerGap(48, Short.MAX_VALUE))
             );
             pack();
         }// </editor-fold>                        
         private void btn_SelectDirectoryActionPerformed(java.awt.event.ActionEvent evt) {                                                   
             JFileChooser fc = new JFileChooser();
             
             fc.setDialogTitle("Select Directory");
             //fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
             fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
             
             int returnVal = fc.showOpenDialog(FileMonitorTest.this);
             
             if (returnVal == JFileChooser.APPROVE_OPTION) {
                     File file = fc.getSelectedFile();              
                     txt_ScannedDirectory.setText(file.getPath());
                 }
             else {
                  
                 }
         }                                                   
         private void btn_StartScanActionPerformed(java.awt.event.ActionEvent evt) {                                             
             String directory = txt_ScannedDirectory.getText();        
             FileMonitor fileMonitor = new FileMonitor(directory, 100);
             tableModel = new FileTableModel(new File(directory));
             fileMonitor.addListener((FileListener)tableModel);
             fileMonitor.start(true);
             
             jTable1.setModel(tableModel);
             
             tableModel.addTableModelListener(new TableModelListener() {
                     public void tableChanged(TableModelEvent e) {                    
                         if(tableModel.getRowCount()>0){
                             txt_FilesCount.setText(String.valueOf(tableModel.getRowCount()));
                         }
                         else{
                             txt_FilesCount.setText("shit,have nothing in it!");
                         }                    
                     }
                 });
         }                                             
         
         /**
          * @param args the command line arguments
          */
         public static void main(String args[]) {
             java.awt.EventQueue.invokeLater(new Runnable() {
                 public void run() {
                     new FileMonitorTest().setVisible(true);
                 }
             });
         }
         
         // Variables declaration - do not modify                     
         private javax.swing.JButton btn_SelectDirectory;
         private javax.swing.JButton btn_StartScan;
         private javax.swing.JLabel jLabel1;
         private javax.swing.JLabel jLabel2;
         private javax.swing.JScrollPane jScrollPane1;
         private javax.swing.JTable jTable1;
         private javax.swing.JTextField txt_FilesCount;
         private javax.swing.JTextField txt_ScannedDirectory;
         // End of variables declaration                  
         
    }  


      
      
       
       

         
       

         
       
      
    复制代码

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

    使用道具 举报

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

    本版积分规则

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

    GMT+8, 2025-2-25 13:07 , Processed in 0.375353 second(s), 46 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

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