Created
October 7, 2010 16:46
-
-
Save MoriTanosuke/615432 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
public class Grep { | |
public static void main(String[] args) throws IOException { | |
if(args.length != 2) { | |
System.err.println("Wrong parameter count!"); | |
System.err.println("USAGE:\tjava Grep pattern file"); | |
System.exit(-1); | |
} | |
String re = ".*" + args[0] + ".*"; | |
String filename = args[1]; | |
File file = new File(filename); | |
BufferedReader in = new BufferedReader(new FileReader(file)); | |
if(file.exists()) { | |
String line = null; | |
int lineNumber = 1; | |
while((line = in.readLine()) != null) { | |
if (line.matches(re)) { | |
System.out.println("L" + lineNumber + ": " + line); | |
} | |
lineNumber++; | |
} | |
} | |
in.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment