<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FFile%2FFile_Monitor</id>
		<title>Java Tutorial/File/File Monitor - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FFile%2FFile_Monitor"/>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/File/File_Monitor&amp;action=history"/>
		<updated>2026-04-07T08:59:44Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/File/File_Monitor&amp;diff=5364&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/File/File_Monitor&amp;diff=5364&amp;oldid=prev"/>
				<updated>2010-06-01T05:19:53Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 05:19, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://jexp.ru/index.php?title=Java_Tutorial/File/File_Monitor&amp;diff=5363&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://jexp.ru/index.php?title=Java_Tutorial/File/File_Monitor&amp;diff=5363&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Monitor files for changes ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
// Copyright (C) 2007 Google Inc.&lt;br /&gt;
//&lt;br /&gt;
// Licensed under the Apache License, Version 2.0 (the &amp;quot;License&amp;quot;);&lt;br /&gt;
// you may not use this file except in compliance with the License.&lt;br /&gt;
// You may obtain a copy of the License at&lt;br /&gt;
//&lt;br /&gt;
// http://www.apache.org/licenses/LICENSE-2.0&lt;br /&gt;
//&lt;br /&gt;
// Unless required by applicable law or agreed to in writing, software&lt;br /&gt;
// distributed under the License is distributed on an &amp;quot;AS IS&amp;quot; BASIS,&lt;br /&gt;
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.&lt;br /&gt;
// See the License for the specific language governing permissions and&lt;br /&gt;
// limitations under the License.&lt;br /&gt;
&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.util.Hashtable;&lt;br /&gt;
import java.util.Timer;&lt;br /&gt;
import java.util.TimerTask;&lt;br /&gt;
/**&lt;br /&gt;
 * Monitor files for changes. This singleton class maintains a map of files to&lt;br /&gt;
 * monitor and objects to notify when something they change.&lt;br /&gt;
 */&lt;br /&gt;
public class FileMonitor {&lt;br /&gt;
  private static final FileMonitor SINGLETON = new FileMonitor();&lt;br /&gt;
  private Timer timer;&lt;br /&gt;
  private Hashtable&amp;lt;String, TimerTask&amp;gt; timerTasks;&lt;br /&gt;
  private FileMonitor() {&lt;br /&gt;
    timer = new Timer(true);&lt;br /&gt;
    timerTasks = new Hashtable&amp;lt;String, TimerTask&amp;gt;();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Returns the singleton instance of this class.&lt;br /&gt;
   * @return the singleton instance&lt;br /&gt;
   */&lt;br /&gt;
  public static FileMonitor getInstance() {&lt;br /&gt;
    return SINGLETON;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Start monitoring a file.&lt;br /&gt;
   * &lt;br /&gt;
   * @param listener listener to notify when the file changed.&lt;br /&gt;
   * @param fileName name of the file to monitor.&lt;br /&gt;
   * @param period polling period in milliseconds.&lt;br /&gt;
   */&lt;br /&gt;
  public void addFileChangeListener(FileChangeListener listener,&lt;br /&gt;
      String fileName, long period) {&lt;br /&gt;
    removeFileChangeListener(listener, fileName);&lt;br /&gt;
    FileMonitorTask task = new FileMonitorTask(listener, fileName);&lt;br /&gt;
    timerTasks.put(fileName + listener.hashCode(), task);&lt;br /&gt;
    timer.schedule(task, period, period);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Remove the listener from the notification list.&lt;br /&gt;
   * &lt;br /&gt;
   * @param listener the listener to be removed.&lt;br /&gt;
   */&lt;br /&gt;
  public void removeFileChangeListener(FileChangeListener listener,&lt;br /&gt;
      String fileName) {&lt;br /&gt;
    FileMonitorTask task = (FileMonitorTask) timerTasks.remove(fileName&lt;br /&gt;
        + listener.hashCode());&lt;br /&gt;
    if (task != null) {&lt;br /&gt;
      task.cancel();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  protected void fireFileChangeEvent(FileChangeListener listener,&lt;br /&gt;
      String fileName) {&lt;br /&gt;
    listener.fileChanged(fileName);&lt;br /&gt;
  }&lt;br /&gt;
  class FileMonitorTask extends TimerTask {&lt;br /&gt;
    FileChangeListener listener;&lt;br /&gt;
    String fileName;&lt;br /&gt;
    File monitoredFile;&lt;br /&gt;
    long lastModified;&lt;br /&gt;
    public FileMonitorTask(FileChangeListener listener, String fileName) {&lt;br /&gt;
      this.listener = listener;&lt;br /&gt;
      this.fileName = fileName;&lt;br /&gt;
      this.lastModified = 0;&lt;br /&gt;
      monitoredFile = new File(fileName);&lt;br /&gt;
      this.lastModified = getLastModified();&lt;br /&gt;
    }&lt;br /&gt;
    private long getLastModified() {&lt;br /&gt;
      if (monitoredFile.exists()) { &lt;br /&gt;
        return monitoredFile.lastModified();&lt;br /&gt;
      } else {&lt;br /&gt;
        return -1;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    @Override&lt;br /&gt;
    public void run() {&lt;br /&gt;
      long lastModified = getLastModified();&lt;br /&gt;
      if (lastModified != this.lastModified) {&lt;br /&gt;
        this.lastModified = lastModified;&lt;br /&gt;
        fireFileChangeEvent(this.listener, this.fileName);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public interface FileChangeListener {&lt;br /&gt;
    public void fileChanged(String fileName);&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Monitoring a File for changes. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*******************************************************************************&lt;br /&gt;
 * Copyright (c) 2007 Pascal Essiembre.&lt;br /&gt;
 * All rights reserved. This program and the accompanying materials&lt;br /&gt;
 * are made available under the terms of the Eclipse Public License v1.0&lt;br /&gt;
 * which accompanies this distribution, and is available at&lt;br /&gt;
 * http://www.eclipse.org/legal/epl-v10.html&lt;br /&gt;
 *&lt;br /&gt;
 * Contributors:&lt;br /&gt;
 *    Pascal Essiembre - initial API and implementation&lt;br /&gt;
 ******************************************************************************/&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileNotFoundException;&lt;br /&gt;
import java.net.URL;&lt;br /&gt;
import java.util.Hashtable;&lt;br /&gt;
import java.util.Timer;&lt;br /&gt;
import java.util.TimerTask;&lt;br /&gt;
/**&lt;br /&gt;
 * Class monitoring a {@link File} for changes.&lt;br /&gt;
 * &lt;br /&gt;
 * @author Pascal Essiembre&lt;br /&gt;
 */&lt;br /&gt;
public class FileMonitor {&lt;br /&gt;
  private static final FileMonitor instance = new FileMonitor();&lt;br /&gt;
  private Timer timer;&lt;br /&gt;
  private Hashtable&amp;lt;String, FileMonitorTask&amp;gt; timerEntries;&lt;br /&gt;
  /**&lt;br /&gt;
   * Gets the file monitor instance.&lt;br /&gt;
   * &lt;br /&gt;
   * @return file monitor instance&lt;br /&gt;
   */&lt;br /&gt;
  public static FileMonitor getInstance() {&lt;br /&gt;
    return instance;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Constructor.&lt;br /&gt;
   */&lt;br /&gt;
  private FileMonitor() {&lt;br /&gt;
    // Create timer, run timer thread as daemon.&lt;br /&gt;
    timer = new Timer(true);&lt;br /&gt;
    timerEntries = new Hashtable&amp;lt;String, FileMonitorTask&amp;gt;();&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Adds a monitored file with a {@link FileChangeListener}.&lt;br /&gt;
   * &lt;br /&gt;
   * @param listener&lt;br /&gt;
   *          listener to notify when the file changed.&lt;br /&gt;
   * @param fileName&lt;br /&gt;
   *          name of the file to monitor.&lt;br /&gt;
   * @param period&lt;br /&gt;
   *          polling period in milliseconds.&lt;br /&gt;
   */&lt;br /&gt;
  public void addFileChangeListener(FileChangeListener listener, String fileName, long period)&lt;br /&gt;
      throws FileNotFoundException {&lt;br /&gt;
    addFileChangeListener(listener, new File(fileName), period);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Adds a monitored file with a FileChangeListener.&lt;br /&gt;
   * &lt;br /&gt;
   * @param listener&lt;br /&gt;
   *          listener to notify when the file changed.&lt;br /&gt;
   * @param fileName&lt;br /&gt;
   *          name of the file to monitor.&lt;br /&gt;
   * @param period&lt;br /&gt;
   *          polling period in milliseconds.&lt;br /&gt;
   */&lt;br /&gt;
  public void addFileChangeListener(FileChangeListener listener, File file, long period)&lt;br /&gt;
      throws FileNotFoundException {&lt;br /&gt;
    removeFileChangeListener(listener, file);&lt;br /&gt;
    FileMonitorTask task = new FileMonitorTask(listener, file);&lt;br /&gt;
    timerEntries.put(file.toString() + listener.hashCode(), task);&lt;br /&gt;
    timer.schedule(task, period, period);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Remove the listener from the notification list.&lt;br /&gt;
   * &lt;br /&gt;
   * @param listener&lt;br /&gt;
   *          the listener to be removed.&lt;br /&gt;
   */&lt;br /&gt;
  public void removeFileChangeListener(FileChangeListener listener, String fileName) {&lt;br /&gt;
    removeFileChangeListener(listener, new File(fileName));&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Remove the listener from the notification list.&lt;br /&gt;
   * &lt;br /&gt;
   * @param listener&lt;br /&gt;
   *          the listener to be removed.&lt;br /&gt;
   */&lt;br /&gt;
  public void removeFileChangeListener(FileChangeListener listener, File file) {&lt;br /&gt;
    FileMonitorTask task = timerEntries.remove(file.toString() + listener.hashCode());&lt;br /&gt;
    if (task != null) {&lt;br /&gt;
      task.cancel();&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Fires notification that a file changed.&lt;br /&gt;
   * &lt;br /&gt;
   * @param listener&lt;br /&gt;
   *          file change listener&lt;br /&gt;
   * @param file&lt;br /&gt;
   *          the file that changed&lt;br /&gt;
   */&lt;br /&gt;
  protected void fireFileChangeEvent(FileChangeListener listener, File file) {&lt;br /&gt;
    listener.fileChanged(file);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * File monitoring task.&lt;br /&gt;
   */&lt;br /&gt;
  class FileMonitorTask extends TimerTask {&lt;br /&gt;
    FileChangeListener listener;&lt;br /&gt;
    File monitoredFile;&lt;br /&gt;
    long lastModified;&lt;br /&gt;
    public FileMonitorTask(FileChangeListener listener, File file) throws FileNotFoundException {&lt;br /&gt;
      this.listener = listener;&lt;br /&gt;
      this.lastModified = 0;&lt;br /&gt;
      monitoredFile = file;&lt;br /&gt;
      if (!monitoredFile.exists()) { // but is it on CLASSPATH?&lt;br /&gt;
        URL fileURL = listener.getClass().getClassLoader().getResource(file.toString());&lt;br /&gt;
        if (fileURL != null) {&lt;br /&gt;
          monitoredFile = new File(fileURL.getFile());&lt;br /&gt;
        } else {&lt;br /&gt;
          throw new FileNotFoundException(&amp;quot;File Not Found: &amp;quot; + file);&lt;br /&gt;
        }&lt;br /&gt;
      }&lt;br /&gt;
      this.lastModified = monitoredFile.lastModified();&lt;br /&gt;
    }&lt;br /&gt;
    public void run() {&lt;br /&gt;
      long lastModified = monitoredFile.lastModified();&lt;br /&gt;
      if (lastModified != this.lastModified) {&lt;br /&gt;
        this.lastModified = lastModified;&lt;br /&gt;
        fireFileChangeEvent(this.listener, monitoredFile);&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
/*******************************************************************************&lt;br /&gt;
 * Copyright (c) 2007 Pascal Essiembre. All rights reserved. This program and&lt;br /&gt;
 * the accompanying materials are made available under the terms of the Eclipse&lt;br /&gt;
 * Public License v1.0 which accompanies this distribution, and is available at&lt;br /&gt;
 * http://www.eclipse.org/legal/epl-v10.html&lt;br /&gt;
 * &lt;br /&gt;
 * Contributors: Pascal Essiembre - initial API and implementation&lt;br /&gt;
 ******************************************************************************/&lt;br /&gt;
/**&lt;br /&gt;
 * Listener interested in {@link File} changes.&lt;br /&gt;
 * &lt;br /&gt;
 * @author Pascal Essiembre&lt;br /&gt;
 */&lt;br /&gt;
interface FileChangeListener {&lt;br /&gt;
  /**&lt;br /&gt;
   * Invoked when a file changes.&lt;br /&gt;
   * &lt;br /&gt;
   * @param fileName&lt;br /&gt;
   *          name of changed file.&lt;br /&gt;
   */&lt;br /&gt;
  public void fileChanged(File file);&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>