Search a word(taken from one file) in another file and give the line as the output
Each line in file1 is a string.
In file2 there will be so many lines that contains even Japanese characters in between.
I am looking for a java program that takes the string in file1 and searches in file2 and gives the line that contains the string to someother file.
Can anyone help me?
good start, additional work needed.
This is good start, much work needed to place work correctly. Suggest work on code correctness first, then work on actual problem. Comments placed in you code as I straighten the code.
Originally Posted by SwapnaNaidu
Much work to do remaining, please work on this some - I have to go to work now on prior commitment.
Will attempt to do some additional work on this tomorrow morning. See what you can come up with.
I did not do a test compile nor check code in detail. This is simple to "proceed further" ... work remains.
import java.io.*;// Needed for files.
import java.util.regex.*;// Very useful for searching.
import java.lang.*;// This is imported for you, but is okay to leave this import here.
public class FileReadTest1
// Use some file type with the word Reader in it to read Strings.
BufferedReader searchFile = null;
BufferedReader dataFile = null;
// Constructor for class.
FileReadTest1() throws IOException
File f1 = new File("SearchFile.txt");
searchFile = new BufferedReader(new InputStreamReader(f1));
File f2 = new File("mydata.txt")
dataFile = new BufferedReader(new InputStreamReader(f2));
public static void main (String args)
FileReadTest1 fileReadTest1 = new FileReadTest1();
System.out.println("U have go an exception 1 " + e.getMessage());
return;// This exits program if file cannot be opened, further work needed here.
void readMyFile() throws IOException
String record = dataFile.readline();
if(record != null)
String resutlsFileName = new String("results.txt");//
// A place to store results....
FileWriter resutlsFile = new FileWriter(resutlsFileName);
String nextWordtoSearchfor = searchFile.readline();
Pattern myPattern = Pattern.compile(nextWordtoSearchfor, CASE_INSENSITIVE);
Matcher matcher = myPattern.matcher(record);//
record = dataFile.readline();
while(record != null);
System.out.println("Internal problem with program, contact system administrator.");
System.out.println("A word in the search file conatins a metacharacter..");