Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- public class FileLists extends Thread {
- List<String> baseText = new ArrayList<String>();
- List<String> newText;
- List<String> fileNameList = new ArrayList<String>();
- String path;
- MailSender soap = new MailSender();
- FileLists(String path) {
- this.path = path;
- fileNameList.add(this.path);
- }
- public void run() {
- Scanner scanner = null;
- try {
- boolean loop = true;
- while (loop == true) {
- Thread.sleep(2000);
- newText = new ArrayList<String>();
- for (String fileNames : fileNameList) {
- scanner = new Scanner(new FileInputStream(fileNames));
- }
- while (scanner.hasNextLine()) {
- newText.add(scanner.nextLine());
- }
- baseText.addAll(newText);
- for (int i = 0; i < newText.size(); i++) {
- if (!baseText.get(i).equals(newText.get(i))) {
- System.out.println("В этом файле - " + path + " произошли изменения.");
- soap.SendMessage(path);
- baseText.clear();
- baseText.addAll(newText);
- return;
- }
- }
- }
- } catch (NullPointerException exx) {
- return;
- } catch (FileNotFoundException ex) {
- System.out.println("Файл по этому адресу -- " + path + " -- не был найден.");
- return;
- } catch (InterruptedException e) {
- System.out.println("Мониторинг файла по этому адресу -- " + path + " -- был остановлен.");
- return;
- } finally {
- if (scanner != null) {
- scanner.close();
- }
- }
- }
- public void MonitorList() {
- for (String fileNames : fileNameList) {
- System.out.print(fileNames + "\n");
- }
- }
- public void ClearMonitorList() {
- fileNameList.clear();
- }
- }
- import java.util.Properties;
- import javax.mail.Message;
- import javax.mail.MessagingException;
- import javax.mail.PasswordAuthentication;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.AddressException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class MailSender {
- String mail = "";
- MailSender(String mail) {
- this.mail = mail;
- }
- public void MailAdress() {
- System.out.println(mail);
- }
- public void SendMessage(String fileAdress) {
- final String userName = "bugiman17@mail.ru";
- final String password = "";
- Properties props = new Properties();
- props.put("mail.smtp.auth", "true");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.host", "smtp.mail.ru");
- props.put("mail.smtp.port", "587");
- Session session = Session.getInstance(props, new javax.mail.Authenticator() {
- protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(userName, password);
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress("bugiman17@mail.ru"));
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(this.mail));
- message.setSubject("Произошли изменения");
- message.setText("Замечены изменения файле по этому адресу -- " + fileAdress);
- Transport.send(message);
- System.out.println("Отправлено сообщение на указанную электронную почту.");
- } catch (AddressException ex) {
- System.out.println("Адрес электронной почты указан не корректно.");
- } catch (MessagingException ex) {
- System.out.println("ddd");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement