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;
- import javax.mail.MessagingException;
- public class FileLists extends Thread {
- List<String> baseText = new ArrayList<String>();
- List<String> newText;
- List<String> fileNameList = new ArrayList<String>();
- String path;
- String mail;
- MailSender soap;
- int t = 0;
- FileLists(String path, String mail) {
- this.path = path;
- this.mail = mail;
- fileNameList.add(this.path);
- soap = new MailSender(this.mail);
- }
- 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));
- }
- if (t == 0) {
- while (scanner.hasNextLine()) {
- baseText.add(scanner.nextLine());
- }
- t++;
- }
- while (scanner.hasNextLine()) {
- newText.add(scanner.nextLine());
- }
- baseText.equals(newText);
- if (baseText.size() == newText.size()) {
- if (!baseText.equals(newText)) {
- 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;
- } catch (MessagingException e) {
- e.printStackTrace();
- } 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.lang.Thread.State;
- import java.util.ArrayList;
- import java.util.InputMismatchException;
- import java.util.List;
- import java.util.Scanner;
- public class Monitor {
- public static void main(String args[]) throws InterruptedException {
- Scanner scanner = new Scanner(System.in);
- List<FileLists> scan = new ArrayList<FileLists>();
- MailSender mail;
- boolean mainMainLoop = true;
- boolean filePathMenu = true;
- boolean mainLoop = true;
- boolean mailMenu = true;
- boolean interrupt = false;
- System.out.print("ВВедите почтовый адрес получателя: ");
- while (mainMainLoop == true) {
- String mailAdres = scanner.next();
- mail = new MailSender(mailAdres);
- boolean mailPath = true;
- while (mailPath == true) {
- try {
- System.out.print("\nВы указали это адрес как адрес получателя - ");
- mail.MailAdress();
- System.out.println("Вы уверены?\n1)Да, перейти в меню выбора файлов для мониторинга.\n2)Нет, ввести другой адрес.");
- int mailInt = scanner.nextInt();
- if (mailInt == 1) {
- System.out.println("Введите путь к файлу:");
- filePathMenu = true;
- }
- if (mailInt == 2) {
- System.out.println("Вы меняете адрес получателя:");
- mail = new MailSender(scanner.next());
- filePathMenu = false;
- }
- } catch (InputMismatchException ex) {
- String inputEx = scanner.next();
- filePathMenu = false;
- System.out.println("\tНекорректный ввод.");
- }
- while (filePathMenu == true) {
- try {
- scan.add(new FileLists(scanner.next(), mailAdres));
- System.out.println("1)Войти в меню мониторинга.\n2)Написать больше путей для мониторинга.");
- int pathNum = scanner.nextInt();
- if (pathNum == 1) {
- mailPath = false;
- filePathMenu = false;
- System.out.println("1)Начать мониторинг файлов.\n2)Меню остановки/список отслеживаемых файлов.\n3)Список введённых файлов.\n4)Выход из программы.");
- }
- if (pathNum == 2) {
- System.out.println("Пишите:");
- }
- } catch (InputMismatchException ex) {
- String inputEx = scanner.next();
- System.out.println("\tНекорректный ввод.");
- }
- }
- }
- while (mainLoop == true) {
- try {
- int monitoringMenu = scanner.nextInt();
- if (monitoringMenu == 1) {
- try {
- for (int a = 0; a < scan.size(); a++) {
- scan.get(a).setDaemon(true);
- scan.get(a).start();
- }
- } catch (IllegalThreadStateException ex) {
- for (int a = 0; a < scan.size(); a++) {
- if (scan.get(a).getState() == State.TERMINATED) {
- System.out.println("Нет нужды этого делать. Но я пытался это сделать весь день:'(");
- }
- if (scan.get(a).getState() == State.TIMED_WAITING) {
- System.out.print("Слежение за этим файлом уже идёт: ");
- scan.get(a).MonitorList();
- }
- }
- }
- }
- if (monitoringMenu == 2) {
- interrupt = true;
- System.out.println("Меню остановки: ВВедите номер файла за которым нужно прекратить слежение.\nВведите 0 для выхода.\n-----------------------------------------");
- for (int a = 0; a < scan.size(); a++) {
- try {
- if (scan.get(a).getState() == State.TIMED_WAITING) {
- System.out.print(a + 1 + ")");
- scan.get(a).MonitorList();
- }
- } catch (NullPointerException ex) {
- }
- }
- while (interrupt == true) {
- try {
- int interruptNum = scanner.nextInt();
- if (interruptNum == 0) {
- System.out.println("1)Начать мониторинг файлов.\n2)Меню остановки/список отслеживаемых файлов.\n3)Список введённых файлов.\n4)Выход из программы.");
- interrupt = false;
- }
- if (interruptNum != 0 || interruptNum < scan.size() - 1) {
- scan.get(interruptNum - 1).interrupt();
- }
- if (interruptNum < 0 || interruptNum > scan.size()) {
- System.out.println("Файла под таким номером не существует");
- }
- } catch (IndexOutOfBoundsException ex) {
- }
- }
- }
- if (monitoringMenu == 3) {
- boolean clearMenu = true;
- while (clearMenu == true) {
- System.out.println("Файлы выбранные для мониторинга:");
- for (int b = 0; b < scan.size(); b++) {
- scan.get(b).MonitorList();
- }
- System.out.println("Нажмите 1 чтобы очистить лист введённых файлов. Нажмите 2 чтобы выйти.");
- int clear = scanner.nextInt();
- if (clear == 1) {
- for (int a = 0; a < scan.size(); a++) {
- scan.get(a).ClearMonitorList();
- }
- System.out.println("Лист очищен.");
- System.out.println("1)Начать мониторинг файлов.\n2)Меню остановки/список отслеживаемых файлов.\n3)Список введённых файлов.\n4)Выход из программы.");
- clearMenu = false;
- }
- if (clear == 2) {
- System.out.println("1)Начать мониторинг файлов.\n2)Меню остановки/список отслеживаемых файлов.\n3)Список введённых файлов.\n4)Выход из программы.");
- clearMenu = false;
- }
- }
- }
- if (monitoringMenu == 4) {
- System.out.println("До свидания.");
- return;
- }
- } catch (InputMismatchException ex) {
- String inputEx = scanner.next();
- System.out.println("\tНекорректный ввод.");
- }
- }
- }
- }
- }
- 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 String getMail() {
- return this.mail;
- }
- public void MailAdress() {
- System.out.println(this.mail);
- }
- public void SendMessage(String fileAdress) throws MessagingException {
- final String userName = "bugiman17@mail.ru";
- final String password = "gfhfvgfvgfv999";
- 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("Я Апасная Ашибка");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement