将当前日期时间(实时)与上次修改的文件进行比较
Comparing current date time(live) with file last modified
我希望我的程序输出这样,无论何时从客户端接收到文本文件,它都会打印 "A new file has been received!"。文本文件将存储在 C:/Users/%UserProfile%/Desktop/ad.
下面的代码是检查一个新的文本文件是否已经被接收到
目录。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.Timer;
public class Test {
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
static int count = 0;
static File[] f = null;
static Date date = new Date();
static Calendar now = null;
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
while(true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
f = new File("C:/Users/roberts/Desktop/ad").listFiles();
int l = f.length;
System.out.println("There are " + l + " files in the directory");
for (int i = 0; i < l; i++) {
System.out.println("file " + i + " date modified " + dateFormat.format(f[i].lastModified()));
}
}
}
}
目录中文件总数的输出初始 = 1 和最后修改
收到文件后目录中文件总数的输出= 2 和最后修改
现在为了打印 "A new file has been received!" 我需要将当前日期时间与文件的最后修改日期时间进行比较。
这就是我所做的
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.Timer;
public class Test {
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
int interval = 1000;
static int count = 0;
static File[] f = null;
static Date date = new Date();
static Calendar now = null;
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
while(true) {
f = new File("C:/Users/roberts/Desktop/ad").listFiles();
int l = f.length;
System.out.println(l);
new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
now = Calendar.getInstance();
for (int i = 0; i < l; i++) {
if(dateFormat.format(now.getTime()).equals(dateFormat.format(f[i].lastModified()))) {
System.out.println("A new file has been received!");
}
}
}
}).start();
try {
Thread.currentThread().join();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
我试过了,但没有打印出来 "A new file has been received!"。请帮助
最好使用 Watch Service
. 监视文件系统更改,现在您正在轮询文件更改,但监视服务使用本机 windows 事件。
首先你只需要创建一个 WatchService
class.
的实例
Path path = Paths.get("c:\mydir");
WatchService service = path.getFileSystem().newWatchService();
接下来您必须决定要监视的事件类型。我想您只想在文件夹中出现新文件时得到通知。
path.register(service, StandardWatchEventKinds.ENTRY_CREATE);
接下来,service
充当事件缓冲区。我建议把这个缓冲区的处理放在一个单独的线程中。
// repeat forever
for (;;)
{
// this will block until there's something inside the buffer
WatchKey key = service.take();
// 1 watchkey can contain multiple events, you need to iterate these.
for (WatchEvent<?> event : key.pollEvents())
{
//todo: handle events
}
}
这是因为您的 while(true)
循环没有循环,所以您检查目录大小的代码只工作一次,变量 l
保持不变。您通过调用 Thread.currentThread().join()
永远阻塞线程(当前线程阻塞直到当前线程停止,即永远)。 Timer
每 1000 毫秒触发一次,但由于 l
没有改变,它找不到新文件。
如果你想简单地观察目录的变化,而不是重新发明轮子,你最好使用标准 JDK class https://docs.oracle.com/javase/8/docs/api/java/nio/file/WatchService.html
我希望我的程序输出这样,无论何时从客户端接收到文本文件,它都会打印 "A new file has been received!"。文本文件将存储在 C:/Users/%UserProfile%/Desktop/ad.
下面的代码是检查一个新的文本文件是否已经被接收到 目录。
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.Timer;
public class Test {
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
static int count = 0;
static File[] f = null;
static Date date = new Date();
static Calendar now = null;
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
while(true) {
try {
Thread.sleep(2000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
f = new File("C:/Users/roberts/Desktop/ad").listFiles();
int l = f.length;
System.out.println("There are " + l + " files in the directory");
for (int i = 0; i < l; i++) {
System.out.println("file " + i + " date modified " + dateFormat.format(f[i].lastModified()));
}
}
}
}
目录中文件总数的输出初始 = 1 和最后修改
收到文件后目录中文件总数的输出= 2 和最后修改
现在为了打印 "A new file has been received!" 我需要将当前日期时间与文件的最后修改日期时间进行比较。
这就是我所做的
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import javax.swing.Timer;
public class Test {
final DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
int interval = 1000;
static int count = 0;
static File[] f = null;
static Date date = new Date();
static Calendar now = null;
public static void main(String[] args) {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
while(true) {
f = new File("C:/Users/roberts/Desktop/ad").listFiles();
int l = f.length;
System.out.println(l);
new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
now = Calendar.getInstance();
for (int i = 0; i < l; i++) {
if(dateFormat.format(now.getTime()).equals(dateFormat.format(f[i].lastModified()))) {
System.out.println("A new file has been received!");
}
}
}
}).start();
try {
Thread.currentThread().join();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
}
我试过了,但没有打印出来 "A new file has been received!"。请帮助
最好使用 Watch Service
. 监视文件系统更改,现在您正在轮询文件更改,但监视服务使用本机 windows 事件。
首先你只需要创建一个 WatchService
class.
Path path = Paths.get("c:\mydir");
WatchService service = path.getFileSystem().newWatchService();
接下来您必须决定要监视的事件类型。我想您只想在文件夹中出现新文件时得到通知。
path.register(service, StandardWatchEventKinds.ENTRY_CREATE);
接下来,service
充当事件缓冲区。我建议把这个缓冲区的处理放在一个单独的线程中。
// repeat forever
for (;;)
{
// this will block until there's something inside the buffer
WatchKey key = service.take();
// 1 watchkey can contain multiple events, you need to iterate these.
for (WatchEvent<?> event : key.pollEvents())
{
//todo: handle events
}
}
这是因为您的 while(true)
循环没有循环,所以您检查目录大小的代码只工作一次,变量 l
保持不变。您通过调用 Thread.currentThread().join()
永远阻塞线程(当前线程阻塞直到当前线程停止,即永远)。 Timer
每 1000 毫秒触发一次,但由于 l
没有改变,它找不到新文件。
如果你想简单地观察目录的变化,而不是重新发明轮子,你最好使用标准 JDK class https://docs.oracle.com/javase/8/docs/api/java/nio/file/WatchService.html