Advertisement
Mary_Loskutova

Tracking

Mar 28th, 2016
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.57 KB | None | 0 0
  1. public class File extends Thread {
  2.  
  3.     ArrayList<String> example;
  4.     String name;
  5.  
  6.     File(String name) {
  7.         this.name = name;
  8.         this.example = new ArrayList<String>();
  9.         this.setDaemon(true);
  10.     }
  11.  
  12.     public static ArrayList<String> readFile(String name) throws FileNotFoundException {
  13.         Scanner scanner = null;
  14.         scanner = new Scanner(new BufferedReader(new FileReader(name)));
  15.         ArrayList<String> fileList = new ArrayList<String>();
  16.         while (scanner.hasNextLine()) {
  17.             fileList.add(scanner.nextLine());
  18.         }
  19.         scanner.close();
  20.         return fileList;
  21.     }
  22.  
  23.     public void observeFiles() throws FileNotFoundException, InterruptedException {
  24.         ArrayList<String> baseFile;
  25.         ArrayList<String> observableFile;
  26.         boolean b = true;
  27.         while (b) {
  28.             baseFile = File.readFile(name);
  29.             Thread.sleep(5000);
  30.             observableFile = File.readFile(name);
  31.             if (!baseFile.equals(observableFile)) {
  32.  
  33.                 System.out.println(
  34.                         "File has been changed. Do you want to send a message by e-mail? Enter 1 to send a message or 2 to continue tracking");
  35.                 boolean c = true;
  36.                 while (c) {
  37.                     try {
  38.                         Scanner scanner = new Scanner(System.in);
  39.                         int input = scanner.nextInt();
  40.                         switch (input) {
  41.                         case 1:
  42.                             System.out.println("Enter adress");
  43.                             String adress = scanner.nextLine();
  44.                             MailSender mailsender = new MailSender();
  45.                             mailsender.setProperties();
  46.                             mailsender.sendMessage(adress);
  47.                             return;
  48.                         case 2:
  49.                             return;
  50.                         default:
  51.                             System.out.print("Wrong input!" + "\n");
  52.  
  53.                             break;
  54.                         }
  55.                     } catch (InputMismatchException ex) {
  56.                         System.out.print("Wrong input!");
  57.  
  58.                     }
  59.                 }
  60.             } else {
  61.                 System.out.println("There are no changes in the file " + name);
  62.             }
  63.             baseFile.clear();
  64.             baseFile.addAll(observableFile);
  65.         }
  66.     }
  67.  
  68.     public void run() {
  69.         try {
  70.             this.observeFiles();
  71.         } catch (FileNotFoundException ex) {
  72.             System.out.print("There are no files with such name!");
  73.             return;
  74.         } catch (InterruptedException ex) {
  75.             System.out.print("Tracking has been stopped");
  76.             return;
  77.         }
  78.     }
  79.  
  80. }
  81.  
  82.     public static void main(String[] args) {
  83.  
  84.         Menu menu = new Menu();
  85.         Scanner sc = new Scanner(System.in);
  86.  
  87.         menu.addEntry(new MenuEntry("Start tracking file") {
  88.             @Override
  89.             public void run() {
  90.                 System.out.print("Enter the file name" + "\n");
  91.                 String entry = sc.nextLine();
  92.                 File file = new File(entry);
  93.                 file.start();
  94.             }
  95.         });
  96.  
  97.         menu.addEntry(new MenuEntry("Stop tracking file") {
  98.             @Override
  99.             public void run() {
  100.             }          
  101.         });
  102.         menu.run();
  103.         sc.close();
  104.  
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement