Search a file by regular expressions
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
public class GrepReader extends BufferedReader {
String pattern;
public GrepReader(Reader in, String pattern) {
super(in);
this.pattern = pattern;
}
public final String readLine() throws IOException {
String line;
do {
line = super.readLine();
} while ((line != null) && line.indexOf(pattern) == -1);
return line;
}
public static void main(String args[]) {
try {
GrepReader in = new GrepReader(new FileReader("GrepReader.java"), "GrepReader");
String line;
while ((line = in.readLine()) != null)
System.out.println(line);
in.close();
} catch (Exception e) {
System.err.println(e);
}
}
}
Home
Java Book
Runnable examples
Java Book
Runnable examples
IO File:
- Compare File Dates
- Compress files using with ZIP
- Concatenate files
- Copy a File with NIO FileChannel and ByteBuffer
- Copy a file with FileReader and FileWriter
- Copy a file with InputStream and OutputStream
- Copy a file and overwrite
- Delete a file
- Delete File Recursively
- Get readable file size
- Move a file
- Rename a file
- Report a file's status
- Search a file by regular expressions
- Touch a file: set File Last Modified Time