Advertisement
Guest User

Untitled

a guest
Mar 29th, 2016
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 10.24 KB | None | 0 0
  1. import java.io.FileInputStream;
  2. import java.io.FileNotFoundException;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Scanner;
  6. import javax.mail.MessagingException;
  7.  
  8. public class FileLists extends Thread {
  9.     List<String> baseText = new ArrayList<String>();
  10.     List<String> newText;
  11.     List<String> fileNameList = new ArrayList<String>();
  12.     String path;
  13.     String mail;
  14.     MailSender soap;
  15.     int t = 0;
  16.  
  17.     FileLists(String path, String mail) {
  18.         this.path = path;
  19.         this.mail = mail;
  20.         fileNameList.add(this.path);
  21.         soap = new MailSender(this.mail);
  22.  
  23.     }
  24.  
  25.     public void run() {
  26.         Scanner scanner = null;
  27.         try {
  28.             boolean loop = true;
  29.             while (loop == true) {
  30.                 Thread.sleep(2000);
  31.                 newText = new ArrayList<String>();
  32.                 for (String fileNames : fileNameList) {
  33.                     scanner = new Scanner(new FileInputStream(fileNames));
  34.                 }
  35.                 if (t == 0) {
  36.                     while (scanner.hasNextLine()) {
  37.                         baseText.add(scanner.nextLine());
  38.                     }
  39.                     t++;
  40.                 }
  41.                 while (scanner.hasNextLine()) {
  42.                     newText.add(scanner.nextLine());
  43.                 }
  44.  
  45.                 baseText.equals(newText);
  46.                 if (baseText.size() == newText.size()) {
  47.                     if (!baseText.equals(newText)) {
  48.                         System.out.println("В этом файле - " + path + " произошли изменения.");
  49.                         soap.SendMessage(path);
  50.                         baseText.clear();
  51.                         baseText.addAll(newText);
  52.                         return;
  53.                     }
  54.                 }
  55.             }
  56.         } catch (NullPointerException exx) {
  57.             return;
  58.         } catch (FileNotFoundException ex) {
  59.             System.out.println("Файл по этому адресу  -- " + path + " -- не был найден.");
  60.             return;
  61.         } catch (InterruptedException e) {
  62.             System.out.println("Мониторинг файла по этому адресу -- " + path + " -- был остановлен.");
  63.             return;
  64.         } catch (MessagingException e) {
  65.             e.printStackTrace();
  66.         } finally {
  67.             if (scanner != null) {
  68.                 scanner.close();
  69.             }
  70.         }
  71.     }
  72.  
  73.     public void MonitorList() {
  74.         for (String fileNames : fileNameList) {
  75.             System.out.print(fileNames + "\n");
  76.         }
  77.     }
  78.  
  79.     public void ClearMonitorList() {
  80.         fileNameList.clear();
  81.     }
  82. }
  83.  
  84.  
  85. import java.lang.Thread.State;
  86. import java.util.ArrayList;
  87. import java.util.InputMismatchException;
  88. import java.util.List;
  89. import java.util.Scanner;
  90.  
  91. public class Monitor {
  92.     public static void main(String args[]) throws InterruptedException {
  93.         Scanner scanner = new Scanner(System.in);
  94.         List<FileLists> scan = new ArrayList<FileLists>();
  95.         MailSender mail;
  96.         boolean mainMainLoop = true;
  97.         boolean filePathMenu = true;
  98.         boolean mainLoop = true;
  99.         boolean mailMenu = true;
  100.         boolean interrupt = false;
  101.         System.out.print("ВВедите почтовый адрес получателя: ");
  102.         while (mainMainLoop == true) {
  103.             String mailAdres = scanner.next();
  104.             mail = new MailSender(mailAdres);
  105.             boolean mailPath = true;
  106.             while (mailPath == true) {
  107.                 try {
  108.                     System.out.print("\nВы указали это адрес как адрес получателя - ");
  109.                     mail.MailAdress();
  110.                     System.out.println("Вы уверены?\n1)Да, перейти в меню выбора файлов для мониторинга.\n2)Нет, ввести другой адрес.");
  111.                     int mailInt = scanner.nextInt();
  112.                     if (mailInt == 1) {
  113.                         System.out.println("Введите путь к файлу:");
  114.                         filePathMenu = true;
  115.                     }
  116.                     if (mailInt == 2) {
  117.                         System.out.println("Вы меняете адрес получателя:");
  118.                         mail = new MailSender(scanner.next());
  119.                         filePathMenu = false;
  120.                     }
  121.                 } catch (InputMismatchException ex) {
  122.                     String inputEx = scanner.next();
  123.                     filePathMenu = false;
  124.                     System.out.println("\tНекорректный ввод.");
  125.                 }
  126.  
  127.                 while (filePathMenu == true) {
  128.                     try {
  129.                         scan.add(new FileLists(scanner.next(), mailAdres));
  130.                         System.out.println("1)Войти в меню мониторинга.\n2)Написать больше путей для мониторинга.");
  131.                         int pathNum = scanner.nextInt();
  132.                         if (pathNum == 1) {
  133.                             mailPath = false;
  134.                             filePathMenu = false;
  135.                             System.out.println("1)Начать мониторинг файлов.\n2)Меню остановки/список отслеживаемых файлов.\n3)Список введённых файлов.\n4)Выход из программы.");
  136.                         }
  137.                         if (pathNum == 2) {
  138.                             System.out.println("Пишите:");
  139.                         }
  140.                     } catch (InputMismatchException ex) {
  141.                         String inputEx = scanner.next();
  142.                         System.out.println("\tНекорректный ввод.");
  143.                     }
  144.                 }
  145.             }
  146.             while (mainLoop == true) {
  147.                 try {
  148.                     int monitoringMenu = scanner.nextInt();
  149.                     if (monitoringMenu == 1) {
  150.                         try {
  151.                             for (int a = 0; a < scan.size(); a++) {
  152.                                 scan.get(a).setDaemon(true);
  153.                                 scan.get(a).start();
  154.                             }
  155.                         } catch (IllegalThreadStateException ex) {
  156.                             for (int a = 0; a < scan.size(); a++) {
  157.                                 if (scan.get(a).getState() == State.TERMINATED) {
  158.                                     System.out.println("Нет нужды этого делать. Но я пытался это сделать весь день:'(");
  159.                                 }
  160.                                 if (scan.get(a).getState() == State.TIMED_WAITING) {
  161.                                     System.out.print("Слежение за этим файлом уже идёт: ");
  162.                                     scan.get(a).MonitorList();
  163.                                 }
  164.                             }
  165.                         }
  166.                     }
  167.                     if (monitoringMenu == 2) {
  168.                         interrupt = true;
  169.                         System.out.println("Меню остановки: ВВедите номер файла за которым нужно прекратить слежение.\nВведите 0 для выхода.\n-----------------------------------------");
  170.                         for (int a = 0; a < scan.size(); a++) {
  171.                             try {
  172.                                 if (scan.get(a).getState() == State.TIMED_WAITING) {
  173.                                     System.out.print(a + 1 + ")");
  174.                                     scan.get(a).MonitorList();
  175.                                 }
  176.                             } catch (NullPointerException ex) {
  177.                             }
  178.                         }
  179.                         while (interrupt == true) {
  180.                             try {
  181.                                 int interruptNum = scanner.nextInt();
  182.                                 if (interruptNum == 0) {
  183.                                     System.out.println("1)Начать мониторинг файлов.\n2)Меню остановки/список отслеживаемых файлов.\n3)Список введённых файлов.\n4)Выход из программы.");
  184.                                     interrupt = false;
  185.                                 }
  186.                                 if (interruptNum != 0 || interruptNum < scan.size() - 1) {
  187.                                     scan.get(interruptNum - 1).interrupt();
  188.                                 }
  189.                                 if (interruptNum < 0 || interruptNum > scan.size()) {
  190.                                     System.out.println("Файла под таким номером не существует");
  191.                                 }
  192.                             } catch (IndexOutOfBoundsException ex) {
  193.                             }
  194.                         }
  195.                     }
  196.                     if (monitoringMenu == 3) {
  197.                         boolean clearMenu = true;
  198.                         while (clearMenu == true) {
  199.                             System.out.println("Файлы выбранные для мониторинга:");
  200.                             for (int b = 0; b < scan.size(); b++) {
  201.                                 scan.get(b).MonitorList();
  202.                             }
  203.                             System.out.println("Нажмите 1 чтобы очистить лист введённых файлов. Нажмите 2 чтобы выйти.");
  204.                             int clear = scanner.nextInt();
  205.                             if (clear == 1) {
  206.                                 for (int a = 0; a < scan.size(); a++) {
  207.                                     scan.get(a).ClearMonitorList();
  208.                                 }
  209.                                 System.out.println("Лист очищен.");
  210.                                 System.out.println("1)Начать мониторинг файлов.\n2)Меню остановки/список отслеживаемых файлов.\n3)Список введённых файлов.\n4)Выход из программы.");
  211.                                 clearMenu = false;
  212.                             }
  213.                             if (clear == 2) {
  214.                                 System.out.println("1)Начать мониторинг файлов.\n2)Меню остановки/список отслеживаемых файлов.\n3)Список введённых файлов.\n4)Выход из программы.");
  215.                                 clearMenu = false;
  216.                             }
  217.                         }
  218.                     }
  219.                     if (monitoringMenu == 4) {
  220.                         System.out.println("До свидания.");
  221.                         return;
  222.                     }
  223.                 } catch (InputMismatchException ex) {
  224.                     String inputEx = scanner.next();
  225.                     System.out.println("\tНекорректный ввод.");
  226.                 }
  227.             }
  228.         }
  229.     }
  230. }
  231.  
  232. import java.util.Properties;
  233. import javax.mail.Message;
  234. import javax.mail.MessagingException;
  235. import javax.mail.PasswordAuthentication;
  236. import javax.mail.Session;
  237. import javax.mail.Transport;
  238. import javax.mail.internet.AddressException;
  239. import javax.mail.internet.InternetAddress;
  240. import javax.mail.internet.MimeMessage;
  241.  
  242. public class MailSender {
  243.     String mail = "";
  244.  
  245.     MailSender(String mail) {
  246.         this.mail = mail;
  247.     }
  248.  
  249.     public String getMail() {
  250.         return this.mail;
  251.     }
  252.  
  253.     public void MailAdress() {
  254.         System.out.println(this.mail);
  255.     }
  256.  
  257.     public void SendMessage(String fileAdress) throws MessagingException {
  258.         final String userName = "bugiman17@mail.ru";
  259.         final String password = "gfhfvgfvgfv999";
  260.  
  261.         Properties props = new Properties();
  262.         props.put("mail.smtp.auth", "true");
  263.         props.put("mail.smtp.starttls.enable", "true");
  264.         props.put("mail.smtp.host", "smtp.mail.ru");
  265.         props.put("mail.smtp.port", "587");
  266.  
  267.         Session session = Session.getInstance(props, new javax.mail.Authenticator() {
  268.             protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
  269.                 return new PasswordAuthentication(userName, password);
  270.             }
  271.         });
  272.         try {
  273.             Message message = new MimeMessage(session);
  274.             message.setFrom(new InternetAddress("bugiman17@mail.ru"));
  275.             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(this.mail));
  276.             message.setSubject("Произошли изменения");
  277.             message.setText("Замечены изменения в файле по этому адресу -- " + fileAdress);
  278.             Transport.send(message);
  279.             System.out.println("Отправлено сообщение на указанную электронную почту.");
  280.         } catch (AddressException ex) {
  281.             System.out.println("Адрес электронной почты указан не корректно.");
  282.         } catch (MessagingException ex) {
  283.             System.out.println("Я Апасная Ашибка");
  284.         }
  285.     }
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement