Advertisement
Mary_Loskutova

File 2

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