2012-01-18 126 views
0

我制作了一个程序,可以持续监控日志文件。但我不知道如何监视多个日志文件。这是我做的监控单个文件。我应该在下面的代码中进行哪些更改,以便它还监视多个文件?同时监控多个日志文件

package com.read; 

import java.io.File; 
import java.io.IOException; 
import java.io.RandomAccessFile; 
import java.nio.channels.FileChannel; 
import java.nio.channels.FileLock; 
import java.util.Date; 
import java.util.Timer; 
import java.util.TimerTask; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import java.util.regex.Matcher; 
import java.util.regex.Pattern; 

public class FileWatcherTest { 

    public static void main(String args[]) { 

     final File fileName = new File("D:/logs/myFile.log"); 

     // monitor a single file 
     TimerTask fileWatcherTask = new FileWatcher(fileName) { 

      long addFileLen = fileName.length(); 
      FileChannel channel; 
      FileLock lock; 
      String a = ""; 
      String b = ""; 

      @Override 
      protected void onChange(File file) { 

       RandomAccessFile access = null; 
       try { 
        access = new RandomAccessFile(file, "rw"); 
        channel = access.getChannel(); 
        lock = channel.lock(); 
        if (file.length() < addFileLen) { 
         access.seek(file.length()); 
        } else { 
         access.seek(addFileLen); 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       String line = null; 
       try { 

        while ((line = access.readLine()) != null) { 

         System.out.println(line); 

        } 

        addFileLen = file.length(); 

       } catch (IOException ex) { 
        Logger.getLogger(FileWatcherTest.class.getName()).log(
          Level.SEVERE, null, ex); 
       } 
       try { 
        lock.release(); 
       } catch (IOException e1) { 
        // TODO Auto-generated catch block 
        e1.printStackTrace(); 
       } // Close the file 

       try { 
        channel.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      } 

     }; 

     Timer timer = new Timer(); 
     // repeat the check every second 
     timer.schedule(fileWatcherTask, new Date(), 1000); 
    } 
} 




package com.read; 

import java.util.*; 
import java.io.*; 

public abstract class FileWatcher extends TimerTask { 

    private long timeStamp; 
    private File file; 
    static String s; 

    public FileWatcher(File file) { 
     this.file = file; 
     this.timeStamp = file.lastModified(); 
    } 

    public final void run() { 
     long timeStamp = file.lastModified(); 

     if (this.timeStamp != timeStamp) { 
      this.timeStamp = timeStamp; 

      onChange(file); 
     } 
    } 

    protected abstract void onChange(File file); 
} 
+0

拉出匿名类和实例两个实例,每个文件一个 – milan 2012-01-18 13:47:59

+0

u能告诉我如何去做吧? – Rookie 2012-01-18 13:51:01

+0

刚刚告诉过你,你想让我为你做或告诉你什么/如何? :) – milan 2012-01-18 13:55:02

回答

1

您应该使用线程。这里有一个很好的教程:

http://docs.oracle.com/javase/tutorial/essential/concurrency/

你会做这样的事情:

public class FileWatcherTest { 

    public static void main(String args[]) { 
     (new Thread(new FileWatcherRunnable("first.log"))).start(); 
     (new Thread(new FileWatcherRunnable("second.log"))).start(); 
    } 

    private static class FileWatcherRunnable implements Runnable { 
     private String logFilePath; 
     // you should inject the file path of the log file to watch 
     public FileWatcherRunnable(String logFilePath) { 
      this.logFilePath = logFilePath; 
     } 
     public void run() { 
      // your code from main goes in here 
     } 
    } 
} 
+0

嘿谢谢你的答案。我会试试这种方式。 – Rookie 2012-01-18 14:06:23

+0

嘿,我试过这个..但它给出了意想不到的结果。 – Rookie 2012-01-19 07:04:12

+1

使用线程可以摆脱TimerTask的东西,只需在循环中执行一个'Thread.sleep(1000)'来观察修改时间。您可能反而想要观看文件的大小。 – Gray 2012-01-19 13:21:11