Advertisement
Guest User

Untitled

a guest
Mar 28th, 2016
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.65 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.  
  7. public class FileLists extends Thread {
  8.     List<String> baseText = new ArrayList<String>();
  9.     List<String> newText;
  10.     List<String> fileNameList = new ArrayList<String>();
  11.     String path;
  12.     MailSender soap = new MailSender();
  13.  
  14.     FileLists(String path) {       
  15.         this.path = path;
  16.         fileNameList.add(this.path);
  17.     }
  18.  
  19.     public void run() {
  20.         Scanner scanner = null;
  21.         try {
  22.             boolean loop = true;
  23.             while (loop == true) {
  24.                 Thread.sleep(2000);
  25.                 newText = new ArrayList<String>();
  26.                 for (String fileNames : fileNameList) {
  27.                     scanner = new Scanner(new FileInputStream(fileNames));
  28.                 }
  29.                 while (scanner.hasNextLine()) {
  30.                     newText.add(scanner.nextLine());
  31.                 }
  32.                 baseText.addAll(newText);
  33.                 for (int i = 0; i < newText.size(); i++) {
  34.                     if (!baseText.get(i).equals(newText.get(i))) {                     
  35.                         System.out.println("В этом файле - " + path + " произошли изменения.");
  36.                         soap.SendMessage(path);
  37.                         baseText.clear();
  38.                         baseText.addAll(newText);
  39.                         return;
  40.                     }
  41.                 }
  42.             }
  43.         } catch (NullPointerException exx) {           
  44.             return;
  45.         } catch (FileNotFoundException ex) {
  46.             System.out.println("Файл по этому адресу  -- " + path + " -- не был найден.");
  47.             return;
  48.         } catch (InterruptedException e) {
  49.             System.out.println("Мониторинг файла по этому адресу -- " + path + " -- был остановлен.");
  50.             return;
  51.         } finally {
  52.             if (scanner != null) {
  53.                 scanner.close();
  54.             }
  55.         }
  56.     }
  57.  
  58.     public void MonitorList() {
  59.         for (String fileNames : fileNameList) {
  60.             System.out.print(fileNames + "\n");
  61.         }
  62.     }
  63.     public void ClearMonitorList() {
  64.         fileNameList.clear();
  65.     }
  66. }
  67.  
  68.  
  69. import java.util.Properties;
  70. import javax.mail.Message;
  71. import javax.mail.MessagingException;
  72. import javax.mail.PasswordAuthentication;
  73. import javax.mail.Session;
  74. import javax.mail.Transport;
  75. import javax.mail.internet.AddressException;
  76. import javax.mail.internet.InternetAddress;
  77. import javax.mail.internet.MimeMessage;
  78.  
  79. public class MailSender {
  80.     String mail = "";
  81.  
  82.     MailSender(String mail) {
  83.         this.mail = mail;      
  84.     }  
  85.  
  86.     public void MailAdress() {
  87.         System.out.println(mail);
  88.     }
  89.     public void SendMessage(String fileAdress) {   
  90.         final String userName = "bugiman17@mail.ru";
  91.         final String password = "";
  92.  
  93.         Properties props = new Properties();
  94.         props.put("mail.smtp.auth", "true");
  95.         props.put("mail.smtp.starttls.enable", "true");
  96.         props.put("mail.smtp.host", "smtp.mail.ru");
  97.         props.put("mail.smtp.port", "587");
  98.  
  99.         Session session = Session.getInstance(props, new javax.mail.Authenticator() {
  100.             protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
  101.                 return new PasswordAuthentication(userName, password);
  102.             }
  103.         });
  104.         try {
  105.             Message message = new MimeMessage(session);
  106.             message.setFrom(new InternetAddress("bugiman17@mail.ru"));
  107.             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(this.mail));
  108.             message.setSubject("Произошли изменения");
  109.             message.setText("Замечены изменения файле по этому адресу -- " + fileAdress);
  110.             Transport.send(message);
  111.             System.out.println("Отправлено сообщение на указанную электронную почту.");
  112.         } catch (AddressException ex) {
  113.             System.out.println("Адрес электронной почты указан не корректно.");
  114.         } catch (MessagingException ex) {
  115.             System.out.println("ddd");
  116.         }
  117.     }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement