Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class File extends Thread {
- ArrayList<String> example;
- String name;
- File(String name) {
- this.name = name;
- this.example = new ArrayList<String>();
- this.setDaemon(true);
- }
- public static ArrayList<String> readFile(String name) throws FileNotFoundException {
- Scanner scanner = null;
- scanner = new Scanner(new BufferedReader(new FileReader(name)));
- ArrayList<String> fileList = new ArrayList<String>();
- while (scanner.hasNextLine()) {
- fileList.add(scanner.nextLine());
- }
- scanner.close();
- return fileList;
- }
- public void observeFiles() throws FileNotFoundException, InterruptedException {
- ArrayList<String> baseFile;
- ArrayList<String> observableFile;
- boolean b = true;
- while (b) {
- baseFile = File.readFile(name);
- Thread.sleep(5000);
- observableFile = File.readFile(name);
- if (!baseFile.equals(observableFile)) {
- System.out.println(
- "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");
- boolean c = true;
- while (c) {
- try {
- Scanner scanner = new Scanner(System.in);
- int input = scanner.nextInt();
- switch (input) {
- case 1:
- System.out.println("Enter adress");
- String adress = scanner.nextLine();
- MailSender mailsender = new MailSender();
- mailsender.setProperties();
- mailsender.sendMessage(adress);
- return;
- case 2:
- return;
- default:
- System.out.print("Wrong input!" + "\n");
- break;
- }
- } catch (InputMismatchException ex) {
- System.out.print("Wrong input!");
- }
- }
- } else {
- System.out.println("There are no changes in the file " + name);
- }
- baseFile.clear();
- baseFile.addAll(observableFile);
- }
- }
- public void run() {
- try {
- this.observeFiles();
- } catch (FileNotFoundException ex) {
- System.out.print("There are no files with such name!");
- return;
- } catch (InterruptedException ex) {
- System.out.print("Tracking has been stopped");
- return;
- }
- }
- }
- public static void main(String[] args) {
- Menu menu = new Menu();
- Scanner sc = new Scanner(System.in);
- menu.addEntry(new MenuEntry("Start tracking file") {
- @Override
- public void run() {
- System.out.print("Enter the file name" + "\n");
- String entry = sc.nextLine();
- File file = new File(entry);
- file.start();
- }
- });
- menu.addEntry(new MenuEntry("Stop tracking file") {
- @Override
- public void run() {
- }
- });
- menu.run();
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement