Last active
February 25, 2016 00:12
-
-
Save Viacheslav77/dc4460a45da8e16b45df to your computer and use it in GitHub Desktop.
Написать монитор, которых хранит дату модификации файлов и выводит сообщения при изменении в файлах, а так же выводит сообщения при добавлении/удалении файлов
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 Monitor2; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.sql.Date; | |
| /*Написать монитор, которых хранит дату модификации файлов и | |
| выводит сообщения при изменении в файлах* .*/ | |
| public class MyClass { | |
| private static class MyEvents implements IFileEvents { | |
| public void onFileAdded(File path) { | |
| System.out.println("File added: " + path); | |
| } | |
| public void onFileDeleted(File path) { | |
| System.out.println("File deleted: " + path); | |
| } | |
| public void onFileModified(File f) { | |
| Date d = new Date(f.lastModified()); | |
| System.out.println("File "+ f + " мodified: " + d); | |
| } | |
| } | |
| public static void main(String[] args) throws IOException { | |
| Monitor m = new Monitor("d:\\1"); | |
| m.setTimeout(2000); | |
| m.setEvents(new MyEvents()); | |
| m.start(); | |
| } | |
| } |
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 Monitor2; | |
| import java.io.File; | |
| import java.util.Date; | |
| public class filePlusData { | |
| File file; | |
| long lm; | |
| public filePlusData(String path){ | |
| file = new File(path); | |
| lm = file.lastModified(); | |
| } | |
| public File getFile(){ | |
| return file; | |
| } | |
| public long getLM(){ | |
| return lm; | |
| } | |
| } |
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 Monitor2; | |
| import java.io.File; | |
| public interface IFileEvents { | |
| void onFileAdded(File path2); | |
| void onFileDeleted(File path2); | |
| void onFileModified(File path2); | |
| } |
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 Monitor2; | |
| import java.lang.InterruptedException; | |
| import java.nio.file.Files; | |
| import java.nio.file.attribute.BasicFileAttributes; | |
| import java.sql.Date; | |
| import java.util.ArrayList; | |
| import java.util.Iterator; | |
| import java.util.List; | |
| import java.io.*; | |
| public class Monitor { | |
| private String path; | |
| private int timeout; | |
| private List<filePlusData> prev = new ArrayList<filePlusData>(); | |
| private List<filePlusData> curr = new ArrayList<filePlusData>(); | |
| private IFileEvents events; | |
| public Monitor(String path) throws IOException { | |
| this.path = path; | |
| createArray(prev); | |
| } | |
| public void start() throws IOException { | |
| while (true) { | |
| createArray(curr); | |
| compareArrays(prev, curr); | |
| prev.clear(); | |
| prev.addAll(curr); | |
| System.out.println("Waiting..."); | |
| try { | |
| Thread.sleep(timeout); | |
| } catch (InterruptedException ex) { | |
| return; | |
| } | |
| } | |
| } | |
| public int getTimeout() { | |
| return timeout; | |
| } | |
| public void setTimeout(int value) { | |
| timeout = value; | |
| } | |
| public IFileEvents getEvents() { | |
| return events; | |
| } | |
| public void setEvents(IFileEvents value) { | |
| events = value; | |
| } | |
| private void doFileAdded(filePlusData path2) { | |
| if (events != null) | |
| events.onFileAdded(path2.getFile()); | |
| } | |
| private void doFileDeleted(filePlusData path2) { | |
| if (events != null) | |
| events.onFileDeleted(path2.getFile()); | |
| } | |
| private void doFileModified(filePlusData path2) { | |
| if (events != null) | |
| events.onFileModified(path2.getFile()); | |
| } | |
| private void compareArrays(List<filePlusData> prev2, List<filePlusData> curr2) throws IOException { | |
| Iterator<filePlusData> it1 = prev2.iterator(); | |
| Iterator<filePlusData> it2 = curr2.iterator(); | |
| filePlusData f1; | |
| filePlusData f2; | |
| if( prev2.size()>curr2.size()){ | |
| while (it1.hasNext()) { | |
| f1 = it1.next(); | |
| f2 = it2.next(); | |
| if ( ! f1.getFile().equals(f2.getFile())){ | |
| doFileDeleted(f1); | |
| return; | |
| } | |
| } | |
| } | |
| if( prev2.size()<curr2.size()){ | |
| while (it2.hasNext()) { | |
| f2 = it2.next(); | |
| f1 = it1.next(); | |
| if ( ! f2.getFile().equals(f1.getFile())){ | |
| doFileAdded(f2); | |
| return; | |
| } | |
| } | |
| } | |
| else { | |
| while (it2.hasNext()) { | |
| f1 = it1.next(); | |
| f2 = it2.next(); | |
| if ( f1.getLM()!=f2.getLM()) | |
| doFileModified(f1); | |
| } | |
| } | |
| } | |
| private void createArray(List<filePlusData> curr2) throws IOException { | |
| File file = new File(path); | |
| File[] list = file.listFiles(); | |
| int i=0; | |
| curr2.clear(); | |
| for (File f : list) { | |
| curr2.add(new filePlusData(f.getCanonicalPath())); | |
| } | |
| } | |
| } |
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
| Waiting... | |
| Waiting... | |
| Waiting... | |
| File added: D:\1\~$D.docx | |
| Waiting... | |
| Waiting... | |
| Waiting... | |
| Waiting... | |
| File D:\1\D.docx мodified: 2016-02-25 | |
| Waiting... | |
| File deleted: D:\1\~$D.docx | |
| Waiting... | |
| Waiting... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment