0

I'm using java.awt.Robot I was wondering if it is possible for java to scan a constantly updating log.txt file and output those key events to an external program.

example: if text.txt has the string "up" within it, java.awt.Robot will output this keyevent externally and interpret it as keyboard arrow up and continue on each time text.txt has been updated.

is it possible for java.awt.Robot to only register certain words like up down left right and ignore any other word.

I am very new to java so apologies if some things are horribly wrong.

this is the scanner script, the scanner script works fine if the log file simply has up, down, left, right etc in it but if its large chucks of paragraphs the script returns null.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerReadFile {

    public static void main(String[] args) {

        // Location of file to read
    File file = new File("text.txt");
    Scanner scanner = null;
    try {
    scanner = new Scanner(file);
    } catch (FileNotFoundException e) {
    e.printStackTrace();
}
    if (scanner != null) {
    String line;
    while (scanner.hasNextLine()) {
    line = scanner.nextLine();
    if ("up".equals(line)) {
      System.out.println(line);
    }
    if ("down".equals(line)) {
      System.out.println(line);
    }
    if ("left".equals(line)) {
      System.out.println(line);
    }
    if ("right".equals(line)) {
      System.out.println(line);
    }   
}
}
}
}

Thanks

eltabo
  • 3,749
  • 1
  • 21
  • 33
  • 2
    Break your problem up into smaller pieces. Can you write a program that reads from an updating text file? Can you write a different program that sends repeated instructions to a Robot instance based on the contents of, say, an ArrayList? Which specific step are you stuck on? – Kevin Workman Feb 19 '14 at 19:01
  • ok thanks, I've edited my post with my file scanner, this is what im stuck at, at the moment. after i have compiled script then put text.log in same directory and run script it returns null. even though the word up is in the text.log. – user3329573 Feb 19 '14 at 20:02
  • If you want to turn `"UP"` into `KeyEvent.VK_UP`, you can use reflection (http://stackoverflow.com/a/17372203/1003886 ) – kajacx Feb 19 '14 at 20:02
  • yeah I've read up on how to output the input with java.awt.Robot. thx tho. – user3329573 Feb 19 '14 at 20:07
  • the scanner script works fine if the log file simply has up, down, left, right etc in it but if its large chucks of paragraphs the script returns null. – user3329573 Feb 19 '14 at 20:12

0 Answers0