-
-
Save Viacheslav77/8ecf0a1c5a50243cbc32 to your computer and use it in GitHub Desktop.
Проект Монитор: 1) функция вывода даты создания файла на экран (см. java.io.File). 2) функция мониторинга более одного файла. 3) . Написать код для мониторинга каталога с функцией рекурсивного мониторинга. Выводить на экран предупреждение если в каталог добавляется текстовый файл (*.txt).
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 Monitor; | |
| import java.io.IOException; | |
| public class Main { | |
| public static void main(String[] args) { | |
| System.out.println( "Добавить в проект «монитор» функцию вывода даты создания файла на экран (см. java.io.File)."); | |
| Monitor m = new Monitor("d:\\1.txt", new FileEvent()); | |
| m.start(); | |
| System.out.println( " \nДобавить в проект ф-ю мониторинга более одного файла."); | |
| m = new Monitor("d:\\1.txt", "d:\\2.txt", new FileEvent()); | |
| m.start1(); | |
| System.out.println( " \nНаписать код для мониторинга каталога. Выводить на экран предупреждение если в каталог \n" | |
| + " добавляется текстовый файл (*.txt). + \n* Дополнить функцией рекурсивного мониторинга каталога."); | |
| m = new Monitor("d:\\1", new FileEvent()); | |
| try { | |
| m.startDir(); | |
| } catch (IOException e) { | |
| // TODO Auto-generated catch block | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
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 Monitor; | |
| import java.io.File; | |
| import java.io.IOException; | |
| import java.sql.Date; | |
| import java.util.ArrayList; | |
| public class Monitor { | |
| String file; | |
| String file1; | |
| IFileEvent event; | |
| public Monitor(String file, IFileEvent event) { | |
| this.file = file; | |
| this.event = event; | |
| } | |
| public Monitor(String file, String file1, IFileEvent event) { | |
| this.file = file; | |
| this.file1 = file1; | |
| this.event = event; | |
| } | |
| protected void findAllList(File [] files, ArrayList<File> list ) throws IOException { | |
| for(File fls: files) | |
| if(fls.isFile()) | |
| list.add(fls); | |
| else { | |
| list.add(fls); | |
| findAllList(fls.listFiles(), list ); | |
| } | |
| } | |
| public void startDir() throws IOException { | |
| File f = new File(file); | |
| File [] lastfiles = f.listFiles(); | |
| ArrayList<File> lastList = new ArrayList <File> (); | |
| findAllList(lastfiles, lastList); | |
| int i = -1; | |
| i++; | |
| //System.out.println(list.get(i)); | |
| System.out.print("Waiting..."); | |
| while (true) { | |
| System.out.print("."); | |
| try { | |
| Thread.sleep(1000); | |
| } catch (InterruptedException e) {} | |
| File [] files = f.listFiles(); | |
| ArrayList<File> list = new ArrayList <File> (); | |
| findAllList(files, list); | |
| String s = files [files.length -1].getName(); | |
| int dotPos = s.lastIndexOf(".") + 1; | |
| s.substring(dotPos); | |
| if (list.size() > lastList.size() && s.substring(dotPos).equals("txt")){ | |
| System.out.println(); | |
| event.onFileAdded(list.get(list.size()-1)); | |
| break; | |
| } | |
| } | |
| } | |
| public void start1() { | |
| while (true) { | |
| File f = new File(file); | |
| File f1 = new File(file1); | |
| int i = 0; | |
| if (f.exists() && f.isFile()) { | |
| if (event != null){ | |
| event.onFileAdded(f); | |
| i++; | |
| } | |
| } | |
| if (f1.exists() && f1.isFile()) { | |
| if (event != null){ | |
| event.onFileAdded(f1); | |
| i++; | |
| } | |
| } | |
| if (i==2) | |
| break; | |
| try { | |
| Thread.sleep(1000); | |
| } catch (InterruptedException e) {} | |
| System.out.println("Waiting..."); | |
| } | |
| } | |
| public void start() { | |
| while (true) { | |
| File f = new File(file); | |
| if (f.exists() && f.isFile()) { | |
| if (event != null) | |
| event.onFileAdded(f); | |
| break; | |
| } | |
| try { | |
| Thread.sleep(1000); | |
| } catch (InterruptedException e) {} | |
| System.out.println("Waiting..."); | |
| } | |
| } | |
| } |
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
| Добавить в проект «монитор» функцию вывода даты создания файла на экран (см. java.io.File). | |
| File added: d:\1.txt -- Last Modified: 2015-05-10 | |
| Добавить в проект ф-ю мониторинга более одного файла. | |
| File added: d:\1.txt -- Last Modified: 2015-05-10 | |
| File added: d:\2.txt -- Last Modified: 2015-05-10 | |
| Написать код для мониторинга каталога. Выводить на экран предупреждение если в каталог | |
| добавляется текстовый файл (*.txt). + | |
| * Дополнить функцией рекурсивного мониторинга каталога. | |
| Waiting.......................... | |
| File added: d:\1\Новый текстовый документ.txt -- Last Modified: 2016-02-24 |
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 Monitor; | |
| import java.io.File; | |
| import java.sql.Date; | |
| public class FileEvent implements IFileEvent { | |
| @Override | |
| public void onFileAdded(File f) { | |
| Date d = new Date(f.lastModified()); | |
| System.out.println("File added: " + f.getPath() + " -- Last Modified: " + d ); | |
| } | |
| } |
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 Monitor; | |
| import java.io.File; | |
| public interface IFileEvent { | |
| void onFileAdded(File f); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment