Improved FileWatch class comment and merged in the interface.
This commit is contained in:
parent
62923e7f60
commit
9215c528f0
3 changed files with 23 additions and 47 deletions
|
|
@ -1,42 +0,0 @@
|
||||||
/*
|
|
||||||
* The MIT License (MIT)
|
|
||||||
*
|
|
||||||
* Copyright (c) 2015 Ziver Koc
|
|
||||||
*
|
|
||||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
||||||
* of this software and associated documentation files (the "Software"), to deal
|
|
||||||
* in the Software without restriction, including without limitation the rights
|
|
||||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
* copies of the Software, and to permit persons to whom the Software is
|
|
||||||
* furnished to do so, subject to the following conditions:
|
|
||||||
*
|
|
||||||
* The above copyright notice and this permission notice shall be included in
|
|
||||||
* all copies or substantial portions of the Software.
|
|
||||||
*
|
|
||||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
||||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
||||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
||||||
* THE SOFTWARE.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package zutil.io.file;
|
|
||||||
|
|
||||||
import java.io.File;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Interface for the FileWatcher class
|
|
||||||
*
|
|
||||||
* @author Ziver
|
|
||||||
*/
|
|
||||||
public interface FileChangeListener{
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This method is called when there is a change in a file
|
|
||||||
*
|
|
||||||
* @param file The file that has changed
|
|
||||||
*/
|
|
||||||
void fileChangedEvent(File file);
|
|
||||||
}
|
|
||||||
|
|
@ -25,20 +25,24 @@
|
||||||
package zutil.io.file;
|
package zutil.io.file;
|
||||||
|
|
||||||
import zutil.io.MultiPrintStream;
|
import zutil.io.MultiPrintStream;
|
||||||
|
import zutil.log.LogUtil;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class calls a given listener
|
* This class checks a file periodically for changes to the
|
||||||
* when a file is changed
|
* last modified date and calls the registered listeners.
|
||||||
*
|
*
|
||||||
* @author Ziver
|
* @author Ziver
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class FileWatcher extends TimerTask{
|
public class FileWatcher extends TimerTask{
|
||||||
|
private static Logger logger = LogUtil.getLogger();
|
||||||
|
|
||||||
private FileChangeListener listener;
|
private FileChangeListener listener;
|
||||||
private long lastChanged;
|
private long lastChanged;
|
||||||
private File file;
|
private File file;
|
||||||
|
|
@ -82,8 +86,23 @@ public class FileWatcher extends TimerTask{
|
||||||
listener.fileChangedEvent(file);
|
listener.fileChangedEvent(file);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
MultiPrintStream.out.println("File Changed: "+file);
|
logger.fine("File was modified ("+file+") but no listeners was registered.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Interface for the FileWatcher class
|
||||||
|
*
|
||||||
|
* @author Ziver
|
||||||
|
*/
|
||||||
|
public interface FileChangeListener{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method is called when there is a change in a file
|
||||||
|
*
|
||||||
|
* @param file The file that has changed
|
||||||
|
*/
|
||||||
|
void fileChangedEvent(File file);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -26,10 +26,9 @@ package zutil.io.file;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.net.URISyntaxException;
|
|
||||||
|
|
||||||
|
|
||||||
public class FileChangedTest implements FileChangeListener{
|
public class FileChangedTest implements FileWatcher.FileChangeListener {
|
||||||
public static void main(String[] args) throws FileNotFoundException{
|
public static void main(String[] args) throws FileNotFoundException{
|
||||||
FileWatcher watcher = new FileWatcher(FileUtil.find("test.txt"));
|
FileWatcher watcher = new FileWatcher(FileUtil.find("test.txt"));
|
||||||
watcher.setListener(new FileChangedTest());
|
watcher.setListener(new FileChangedTest());
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue