import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.InputStreamReader; import java.util.regex.Matcher; import java.util.regex.Pattern; public class FileSearcher { public static void main(String[] args) { if (args.length == 0) { System.out.println("You must specify a filename!"); return; } String contents = null; try { DataInputStream in = new DataInputStream(new FileInputStream(args[0])); byte[] array = new byte[in.available()]; in.readFully(array); in.close(); contents = new String(array); } catch (Exception e) { e.printStackTrace(); } if (contents == null) { System.out.println("File was not valid"); return; } BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); String userInput = null; do { try { System.out.println("Please type a pattern: "); userInput = in.readLine(); Pattern pattern = Pattern.compile(userInput); Matcher matcher = pattern.matcher(contents); int count = 0; while (matcher.find()) { count++; System.out.println("Result " + count + ": \"" + matcher.group() + "\"\t\tPosition: " + matcher.start()); } System.out.println("Enter another pattern? (y/n)"); userInput = in.readLine(); } catch (Exception e) { e.printStackTrace(); } } while (userInput.charAt(0) == 'y'); } }