View Single Post
  #19 (permalink)  
Old 04-21-2008, 06:40 PM
DonCash's Avatar
DonCash DonCash is offline
Moderator
 
Join Date: Aug 2007
Location: London, UK
Posts: 240
DonCash will become famous soon enoughDonCash will become famous soon enough
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:

Code:
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:

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:

Code:
10/10/2008,server1,xxx.xxx.xxx.xxx 10/10/2008,server2,xxx.xxx.xxx.xxx
__________________
Did this post help you? Please
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
me!

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Last edited by DonCash : 04-21-2008 at 06:53 PM.
Reply With Quote