View Single Post
  #12 (permalink)  
Old 04-21-2008, 02:18 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
You need something like this:

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 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.
__________________
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:23 PM. Reason: Making a few corrections...
Reply With Quote