- 浏览: 2539142 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Monitor Directories and File(II)Timer and commons-vfs
1. Timer and TimerTask
The Interface FileChangeListener.java:
package com.xxxx.importdata.filemonitor.timer;
public interface FileChangeListener
{
public void fileChanged(String filename);
}
The ClassFileChangeListener.java:
package com.xxxxx.importdata.filemonitor.timer;
public class ClassFileChangeListener implements FileChangeListener {
public void fileChanged(String filename) {
System.out
.println("File " + filename + " modified ,it must reload !");
}
}
The most import class FileMonitor.java:
package com.xxxxx.importdata.filemonitor.timer;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
public class FileMonitor
{
private static final FileMonitor instance = new FileMonitor();
private Timer timer;
private Map<String, FileMonitorTask> timerEntries;
private FileMonitor()
{
this.timerEntries = new HashMap<String, FileMonitorTask>();
this.timer = new Timer();
}
public static FileMonitor getInstance()
{
return instance;
}
public void addFileChangeListener(FileChangeListener listener, String filename, long period)
{
this.removeFileChangeListener(filename);
FileMonitorTask task = new FileMonitorTask(listener, filename);
this.timerEntries.put(filename, task);
// schedule(TimerTask task,long delay,long period)
// schedule(TimerTask task,long delay)
// schedule(TimerTask task,Date time)
// schedule(TimerTask task,Date firstTime,long period)
this.timer.scheduleAtFixedRate(task, period, period);
}
public void removeFileChangeListener(String filename)
{
FileMonitorTask task = (FileMonitorTask) this.timerEntries.remove(filename);
if (task != null)
{
task.cancel();
}
}
private static class FileMonitorTask extends TimerTask
{
private FileChangeListener listener;
private String filename;
private File monitoredFile;
private long lastModified;
public FileMonitorTask(FileChangeListener listener, String filename)
{
this.listener = listener;
this.filename = filename;
this.monitoredFile = new File(filename);
if (!this.monitoredFile.exists())
{
return;
}
this.lastModified = this.monitoredFile.lastModified();
}
public void run()
{
long latestChange = this.monitoredFile.lastModified();
if (this.lastModified != latestChange)
{
this.lastModified = latestChange;
this.listener.fileChanged(this.filename);
}
}
}
public static void main(String[] args)
{
String path = "/home/luohua/tmp/t1.txt";
//String path = "/home/luohua/life/work/xxxxx/xxxx-gen/records/";
FileMonitor.getInstance().addFileChangeListener(new ClassFileChangeListener(), path, 2000);
}
}
I am using Linux system, I notice that this lastModified can only detect the file changes. But if there are some changes in the
sub directory. It can not detect that.
And one more thing, it is said that the Timer will cost a lot. I will check if there is another better way.
2. commons-vfs
ivy.xml:
<dependency org="commons-vfs" name="commons-vfs" rev="1.0" />
the test java class is VFSMonitor.java:
package com.xxxxx.importdata.filemonitor.vfs;
import org.apache.commons.vfs.FileChangeEvent;
import org.apache.commons.vfs.FileListener;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.impl.DefaultFileMonitor;
public class VFSMonitor
{
private static final String PATH = "/home/luohua/life/work/xxxxx/xxxx-gen/records/";
public static void main(String[] args)
{
FileSystemManager fsManager = null;
FileObject listendir = null;
try
{
fsManager = VFS.getManager();
listendir = fsManager.resolveFile(PATH);
}
catch (FileSystemException e)
{
e.printStackTrace();
}
DefaultFileMonitor fm = new DefaultFileMonitor(new FileListener() {
public void fileCreated(FileChangeEvent event) throws Exception
{
monitor(event);
}
public void fileDeleted(FileChangeEvent event) throws Exception
{
monitor(event);
}
public void fileChanged(FileChangeEvent event) throws Exception
{
monitor(event);
}
private void monitor(FileChangeEvent event)
{
FileObject fileObject = event.getFile();
FileName fileName = fileObject.getName();
System.out.println(fileName + " has changed.");
}
});
fm.setRecursive(true);
fm.addFile(listendir);
fm.start();
try
{
Thread.sleep(1000000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
fm.stop();
}
}
references:
http://www.iteye.com/topic/1096698
http://www.blogjava.net/quaff/archive/2006/03/02/33229.html
1. Timer and TimerTask
The Interface FileChangeListener.java:
package com.xxxx.importdata.filemonitor.timer;
public interface FileChangeListener
{
public void fileChanged(String filename);
}
The ClassFileChangeListener.java:
package com.xxxxx.importdata.filemonitor.timer;
public class ClassFileChangeListener implements FileChangeListener {
public void fileChanged(String filename) {
System.out
.println("File " + filename + " modified ,it must reload !");
}
}
The most import class FileMonitor.java:
package com.xxxxx.importdata.filemonitor.timer;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
public class FileMonitor
{
private static final FileMonitor instance = new FileMonitor();
private Timer timer;
private Map<String, FileMonitorTask> timerEntries;
private FileMonitor()
{
this.timerEntries = new HashMap<String, FileMonitorTask>();
this.timer = new Timer();
}
public static FileMonitor getInstance()
{
return instance;
}
public void addFileChangeListener(FileChangeListener listener, String filename, long period)
{
this.removeFileChangeListener(filename);
FileMonitorTask task = new FileMonitorTask(listener, filename);
this.timerEntries.put(filename, task);
// schedule(TimerTask task,long delay,long period)
// schedule(TimerTask task,long delay)
// schedule(TimerTask task,Date time)
// schedule(TimerTask task,Date firstTime,long period)
this.timer.scheduleAtFixedRate(task, period, period);
}
public void removeFileChangeListener(String filename)
{
FileMonitorTask task = (FileMonitorTask) this.timerEntries.remove(filename);
if (task != null)
{
task.cancel();
}
}
private static class FileMonitorTask extends TimerTask
{
private FileChangeListener listener;
private String filename;
private File monitoredFile;
private long lastModified;
public FileMonitorTask(FileChangeListener listener, String filename)
{
this.listener = listener;
this.filename = filename;
this.monitoredFile = new File(filename);
if (!this.monitoredFile.exists())
{
return;
}
this.lastModified = this.monitoredFile.lastModified();
}
public void run()
{
long latestChange = this.monitoredFile.lastModified();
if (this.lastModified != latestChange)
{
this.lastModified = latestChange;
this.listener.fileChanged(this.filename);
}
}
}
public static void main(String[] args)
{
String path = "/home/luohua/tmp/t1.txt";
//String path = "/home/luohua/life/work/xxxxx/xxxx-gen/records/";
FileMonitor.getInstance().addFileChangeListener(new ClassFileChangeListener(), path, 2000);
}
}
I am using Linux system, I notice that this lastModified can only detect the file changes. But if there are some changes in the
sub directory. It can not detect that.
And one more thing, it is said that the Timer will cost a lot. I will check if there is another better way.
2. commons-vfs
ivy.xml:
<dependency org="commons-vfs" name="commons-vfs" rev="1.0" />
the test java class is VFSMonitor.java:
package com.xxxxx.importdata.filemonitor.vfs;
import org.apache.commons.vfs.FileChangeEvent;
import org.apache.commons.vfs.FileListener;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.VFS;
import org.apache.commons.vfs.impl.DefaultFileMonitor;
public class VFSMonitor
{
private static final String PATH = "/home/luohua/life/work/xxxxx/xxxx-gen/records/";
public static void main(String[] args)
{
FileSystemManager fsManager = null;
FileObject listendir = null;
try
{
fsManager = VFS.getManager();
listendir = fsManager.resolveFile(PATH);
}
catch (FileSystemException e)
{
e.printStackTrace();
}
DefaultFileMonitor fm = new DefaultFileMonitor(new FileListener() {
public void fileCreated(FileChangeEvent event) throws Exception
{
monitor(event);
}
public void fileDeleted(FileChangeEvent event) throws Exception
{
monitor(event);
}
public void fileChanged(FileChangeEvent event) throws Exception
{
monitor(event);
}
private void monitor(FileChangeEvent event)
{
FileObject fileObject = event.getFile();
FileName fileName = fileObject.getName();
System.out.println(fileName + " has changed.");
}
});
fm.setRecursive(true);
fm.addFile(listendir);
fm.start();
try
{
Thread.sleep(1000000);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
fm.stop();
}
}
references:
http://www.iteye.com/topic/1096698
http://www.blogjava.net/quaff/archive/2006/03/02/33229.html
发表评论
-
Stop Update Here
2020-04-28 09:00 310I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 465NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 361Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 362Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 328Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 419Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 428Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 364Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 444VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 376Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 462NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 413Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 330Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 242GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 443GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 320GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 306Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 310Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 284Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 302Serverless with NodeJS and Tenc ...
相关推荐
在IT领域,尤其是在Java编程中,FTP(File Transfer Protocol)客户端是用于与远程服务器进行文件传输的重要工具。本文将深入探讨使用Java实现FTP客户端时,如何利用`commons-net-1.4.1.jar`和`jakarta-oro-2.0.8....
Files.createDirectories(outputFile.getParentFile().toPath()); Files.copy(zis, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); } zis.close(); ``` 除了ZIP、GZIP和7z,Apache Commons ...
4. **目录管理**:`FTPClient.changeWorkingDirectory()`可切换当前工作目录,`FTPClient.listDirectories()`和`FTPClient.listFiles()`分别用于获取目录列表和文件列表。 5. **日志输出**:Apache Commons Net支持...
3. **目录操作**:你可以使用`changeWorkingDirectory()`方法切换当前工作目录,`listDirectories()`和`listFiles()`方法获取目录和文件信息。 4. **设置传输模式**:`FTPClient`支持主动模式和被动模式,可以通过`...
在Android平台上,FTP(File Transfer Protocol)是一种常用的技术,用于在服务器和客户端之间传输文件。本文将详细讲解如何在Android应用中实现FTP的文件上传和下载功能,同时支持对文件夹的操作。 首先,我们需要...
Logging and input file: -o, --output-file=FILE log messages to FILE. -a, --append-output=FILE append messages to FILE. -d, --debug print debug output. -q, --quiet quiet (no output). -v, --...
Logging and input file: -o, --output-file=FILE log messages to FILE. -a, --append-output=FILE append messages to FILE. -d, --debug print lots of debugging information. -q, --quiet quiet (no output...
Logging and input file: -o, --output-file=FILE log messages to FILE. -a, --append-output=FILE append messages to FILE. -d, --debug print debug output. -q, --quiet quiet (no output). -v, --...
To build the operating system, a batch file (BUILD.BAT) is included to make life easier. This file is in the dos-c directory of the distribution. In addition, there is a corresponding batch file ...
-oneline: display one entry per line-a, --all: show dot files-b, --binary: use binary (power of two) file sizes-B, --bytes: list file sizes in bytes, without prefixes-d, --list-dirs: list directories ...
will also append directories and files to pre-existing squashfs filesystems, see the following 'appending to squashfs filesystems' subsection. SYNTAX:mksquashfs source1 source2 ... dest [options] [-e...
and FILE may be a file or a device. *This* `tar' defaults to `-f- -b20'. Report bugs to <tar-bugs@gnu.org>. --------------------------------- @echo off copy temp"*20090424*.dat tar"bin"tar -cvf ftp...
Directories - Reading and sorting directories. readdir.c - Reading a directory (readdir). dirsortsize.c - Sort a directory by file size (scandir). dirsortalpha.c - Sort a directory alphabetically ...
3. 如果使用IntelliJ IDEA,File -> Project Structure -> Modules -> Dependencies -> '+' -> JARs or directories,选择jar包。 4. 最后,确保在代码中正确引用jar包中的Base64类,即可解决报错问题。 通过以上...
These routines allow programs access to the data streams connected to remote files and directories. FtpAccess() - Open a remote file or directory FtpRead() - Read from remote file or directory Ftp...
- **File System Navigation**: The book explores the Ubuntu file system, explaining its structure and how to navigate through directories and files efficiently. - **File Manager Overview**: A thorough ...
- **File Operations**: Creating, moving, copying, and deleting files and directories using commands like `cp`, `mv`, `rm`, and `mkdir`. - **File Permissions**: Understanding and modifying file ...
在Bash中浏览文件和目录学习目标用ls shell中的目录文件使用mv移动或重命名文件和目录使用cp复制文件touch创建空文件用mkdir新建目录用rm删除文件介绍在上一课中,我们学习了如何“导航”文件系统的目录结构。...
SQUASHFS 2.2 - A squashed read-only filesystem for Linux ...exlude files/directories from the specified exclude file, one file/directory per line. If an exclude file/directory is absolute (i.e. prefixed...
2. **IntelliJ IDEA**:类似地,选择 File -> Project Structure -> Modules -> Dependencies -> '+' -> JARs or directories 添加JAR文件。 3. **Maven/Gradle**:在pom.xml(Maven)或build.gradle(Gradle)中...