Created
March 9, 2016 13:39
-
-
Save Viacheslav77/80f255b8086812033e81 to your computer and use it in GitHub Desktop.
Написать без использования итераторов проект монитора так, чтобы файлы старше N минут автоматически удалялись. 2. Дополнить проект монитора ф-й вывода расширенной информации о файлах на экран (дата создания, размер…).*/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package Monitor3x; | |
| import java.lang.Thread; | |
| import java.lang.InterruptedException; | |
| import java.util.HashMap; | |
| import java.util.Iterator; | |
| import java.util.Set; | |
| import java.util.function.BiConsumer; | |
| 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); | |
| curr.forEach(new BiConsumer <String, Long> (){ | |
| public void accept(String path, Long l) { | |
| long t=System.currentTimeMillis(); | |
| if ((t-l)>timeRemove) | |
| doRemoveFile(path); | |
| if (prev.containsKey(path)){ | |
| if (prev.get(path)< l) | |
| try { | |
| doFileChanged(path); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| else | |
| doFileAdded(path); | |
| } | |
| }); | |
| prev.forEach(new BiConsumer <String, Long> (){ | |
| public void accept(String path, Long l) { | |
| 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; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| File added: D:\2\Документ Microsoft Word.docx 0 byte 09.03.2016 15:33:28 | |
| File added: D:\2\~$кумент Microsoft Word.docx 162 byte 09.03.2016 15:33:34 | |
| File added: D:\2\~WRL1760.tmp 0 byte 09.03.2016 15:33:28 | |
| File changed: D:\2\Документ Microsoft Word.docx 12693 byte 09.03.2016 15:33:46 | |
| File deleted: D:\2\~WRL1760.tmp | |
| File deleted: D:\2\~$кумент Microsoft Word.docx | |
| Delite file D:\2\Документ Microsoft Word.docx | |
| File deleted: D:\2\Документ Microsoft Word.docx |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment