Try this code. This will read in the input file (input.txt) and output whatever you set as FindMe to output.txt. Note: Make sure input.txt is in the same location as the code.
Example input file:
10/10/2008,server1,xxx.xxx.xxx.xxx
10/10/2008,server2,xxx.xxx.xxx.xxx
12/04/2008,server1,xxx.xxx.xxx.xxx
16/07/2008,server1,xxx.xxx.xxx.xxx
Code:
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 Inputpath;
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(Inputpath);
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();
Inputpath = "input.txt";
FindMe = "10/10/2008";
sf.openFile();
}
}
This code is looking for: 10/10/2008
So the output will be:
10/10/2008,server1,xxx.xxx.xxx.xxx
10/10/2008,server2,xxx.xxx.xxx.xxx