SelectRows.javaFrom WikiJavabuy this book SelectRows.java reads a file from the disk and outputs on standard output the lines in the file containing the given string. It is basically a very simple implementation of grep. This Program can be executed by java SelectRows <string> <filename>
the articleSelect Rows opens a file for reading using java.io.bufferedReader: bufferedReader = new BufferedReader(new FileReader(file)); then goes through all the lines of the file: while ((cur = bufferedReader.readLine()) != null) { and prompts them if they contain the string. if (cur.contains(string)) System.out.println(cur.trim());
SelectRows.javapackage com.wikijava.files.manager; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; /** * @author Giulio * */ public class SelectRows { /** * @param args */ public static void main(String[] args) { if (args.length != 2) { System.out.println("usage: java -jar SelectRows \"string\" file"); return; } File file = new File(args[1]); String string = args[0]; BufferedReader bufferedReader = null; try { bufferedReader = new BufferedReader(new FileReader(file)); String cur = null; while ((cur = bufferedReader.readLine()) != null) { if (cur.contains(string)) System.out.println(cur.trim()); } bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } }
See AlsoCopy of a File Tutorial, FileToString.java |
