You need something like this:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.Writer;
public class StringFind {
public static String path;
public static String FindMe;
public String strLine;
public void openFile(){
String newLine = System.getProperty("line.separator");
try {
Writer output = new BufferedWriter(new FileWriter("output.txt"));
FileInputStream in = new FileInputStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((strLine = br.readLine()) != null){
if (strLine.contains(FindMe)) {
output.write(strLine);
output.write(newLine);
}
}
output.close();
System.out.println("Completed. Output file generated.");
}catch(Exception e) {
System.out.println("OUCH! I fell over.");
System.exit(0);
}
}
public static void main(String[] args) {
StringFind sf = new StringFind();
if (args.length < 2){
System.out.println("ERROR! No parameters sent");
System.exit(0);
}
//path = "myFile.txt";
//FindMe = "10-10-2008";
path = args[0];
FindMe = args[1];
sf.openFile();
}
}
This code takes 2 parameters. The first is the file path eg: myfile.txt and the second is the string you are looking for.
As it reads the file it will write all the lines that match FindMe to the output.txt file.