Skip to content

Instantly share code, notes, and snippets.

@Viacheslav77
Created March 4, 2016 22:56
Show Gist options
  • Select an option

  • Save Viacheslav77/e0d49818fecc5453f0e6 to your computer and use it in GitHub Desktop.

Select an option

Save Viacheslav77/e0d49818fecc5453f0e6 to your computer and use it in GitHub Desktop.
Изменить проект монитора так, чтобы файлы старше N минут автоматически удалялись. 2. Дополнить проект монитора ф-й вывода расширенной информации о файлах на экран (размер…).
package Monitor3x;
import java.lang.Thread;
import java.lang.InterruptedException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.io.*;
public class Monitor
{
private class MonitorThread extends Thread {
private String path;
private int timeout;
private HashMap<String, Long> prev = new HashMap<String, Long>();
private HashMap<String, Long> curr = new HashMap<String, Long>();
private IFileEvents events;
private int timeRemove;
public MonitorThread(String path) {
this.path = path;
createHashMap(prev);
}
public void run() {
while ( ! isInterrupted()) {
try {
compareMaps();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
prev.clear();
prev.putAll(curr);
try {
Thread.sleep(timeout);
} catch (InterruptedException ex) {
return;
}
}
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int value) {
timeout = value;
}
public void setRemove(int value) {
timeRemove = value;
}
public IFileEvents getEvents() {
return events;
}
public void setEvents(IFileEvents value) {
events = value;
}
private void doFileChanged(String path) throws IOException {
if (events != null)
events.onFileChanged(path);
}
private void doFileAdded(String path) {
if (events != null)
events.onFileAdded(path);
}
private void doFileDeleted(String path) {
if (events != null)
events.onFileDeleted(path);
}
private void doRemoveFile(String path) {
if (events != null)
events.onRemoveFile(path);
}
private void compareMaps() throws IOException {
createHashMap(curr);
Set<String> keys = curr.keySet();
Iterator<String> it = keys.iterator();
long t=System.currentTimeMillis();
while (it.hasNext())
{
String path = it.next();
long date = t - curr.get(path);
if (date>timeRemove)
doRemoveFile(path);
if (prev.containsKey(path)) {
long date1 = prev.get(path);
long date2 = curr.get(path);
if (date2 > date1)
doFileChanged(path);
} else {
doFileAdded(path);
}
}
keys = prev.keySet();
it = keys.iterator();
while (it.hasNext())
{
String path = it.next();
if ( ! curr.containsKey(path))
doFileDeleted(path);
}
}
private void createHashMap(HashMap<String, Long> output) {
try {
File file = new File(path);
File[] list = file.listFiles();
output.clear();
for (File f : list) {
output.put(f.getCanonicalPath(), new Long(f.lastModified()));
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
// ---------------------------------- //
private String path;
private MonitorThread thread;
private IFileEvents events;
private int timeout = 1000;
private int timeRemove;
public Monitor(String path) {
this.path = path;
}
public void start() {
thread = new MonitorThread(path);
thread.setTimeout(timeout);
thread.setRemove(timeRemove);
thread.setEvents(events);
thread.start();
}
public void stop() {
thread.interrupt();
}
public int getTimeout() {
return timeout;
}
public void setTimeout(int value) {
timeout = value;
}
public void setRemove(int value) {
timeRemove = value;
}
public IFileEvents getEvents() {
return events;
}
public void setEvents(IFileEvents value) {
events = value;
}
}
File added: D:\2\~$Тест.docx 162 byte 05.03.2016 00:47:12
File changed: D:\2\Тест.docx 12779 byte 05.03.2016 00:47:20
File deleted: D:\2\~$Тест.docx
File added: D:\2\~$Тест.docx 162 byte 05.03.2016 00:48:08
File changed: D:\2\Тест.docx 12758 byte 05.03.2016 00:48:23
File deleted: D:\2\~$Тест.docx
Delite file D:\2\Тест.docx
File deleted: D:\2\Тест.docx
package Monitor3x;
import java.io.IOException;
public interface IFileEvents {
void onFileChanged(String path) throws IOException;
void onFileAdded(String path);
void onFileDeleted(String path);
void onRemoveFile(String path);
}
package Monitor3x;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.text.SimpleDateFormat;
/*1.Изменить проект монитора так, чтобы файлы старше N минут автоматически удалялись.
2. Дополнить проект монитора ф-й вывода расширенной информации о файлах на экран (размер…).*/
public class MyClass {
private static BasicFileAttributes attrs = null;
private static SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy" + " HH:mm:ss") ;
static class MyStop extends Thread {
private Monitor s;
public MyStop(Monitor s) {
this.s = s;
}
public void run() {
s.stop();
}
}
private static class MyEvents implements IFileEvents {
public void onFileChanged(String path) {
File file = new File(path);
Path path1 = file.toPath();
try {
attrs = Files.readAttributes(path1, BasicFileAttributes.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File changed: " + path + " " + attrs.size() + " byte " + sdf.format(file.lastModified()));
}
public void onFileAdded(String path) {
File file = new File(path);
Path path1 = file.toPath();
try {
attrs = Files.readAttributes(path1, BasicFileAttributes.class);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("File added: " + path + " " + attrs.size() + " byte " + sdf.format(file.lastModified()));
}
public void onFileDeleted(String path) {
System.out.println("File deleted: " + path);
}
public void onRemoveFile(String path) {
File file = new File(path);
if(file.isFile()){
file.delete();
System.out.println("Delite file " + path);
}
}
}
public static void main(String[] args) {
Monitor m = new Monitor("d:\\2");
m.setTimeout(2000);
m.setRemove(60000*4);
m.setEvents(new MyEvents());
m.start();
Runtime.getRuntime().addShutdownHook(new MyStop(m));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment