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?

Hi Nicholas,
Thanks for your sugestions.
I have worked based on your input.
one more thing I want to make clear is I am looking for the complete line that contains the searched string.
For Ex:
If mydata.txt contains Nicholas and if my SearchFile.txt contains "Hi this is Nicholas"
I want to see "Hi this is Nicholas" in my results.txt file.

The code I have currently is

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.

No § 8's here, boss.

This is good work for first program. I have similar module I worked on in my program. We can read all the words that are being searched for and do HashSet.contains() but that seems to invert the logic - I am still contemplating that. For now I am thinking the approach you are using ( Pattern / Matcher .find() on readline() ) is better. There are additonal improvements that we may be able to find, first we get the program to run.

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.
/**
* The code here is a discussion in techincal fora.
* You may study the code, any use beyond study is
* not acceptable under reasonable risk management
* and would be non-comliance Section 404 Sarbanes-Oxley Act.
*/
public class FileReadTest1
{
// Consistent results file name may be replaced later.
String resutlsFileName = new String("results.txt");//
// Use some file type with the word Reader in it to read Strings.
BufferedReader searchFile = null;
BufferedReader dataFile = null;
// A place to store results....
FileWriter resultsFile = null;//
// Constructor for class.
FileReadTest1() throws IOException
{
// File to be examined.
File f1 = new File("SearchFile.txt");
searchFile = new BufferedReader(new FileReader(f1));
// Keyword file.
File f2 = new File("mydata.txt");
dataFile = new BufferedReader(new FileReader(f2));
// Make results of run available after program finishes.
resultsFile = new FileWriter(resutlsFileName);
}
public static void main (String[] args)
{
try
{
FileReadTest1 fileReadTest1 = new FileReadTest1();
fileReadTest1.readMyFile();
}
catch(IOException ioe)
{
System.out.println("Program has failed to open files needed." + ioe.getMessage());
return;// This exits program if file cannot be opened, further work needed here.
}
catch(Throwable unspecifiedProblem)
{
System.out.println("An internal error occured, do not continue." + unspecifiedProblem.getMessage());
return;// This exits program if file cannot be opened, further work needed here.
}
}
void readMyFile() throws IOException
{
try
{
String record = dataFile.readLine();
if(record != null)
{
int counter = 0x0000;//
String nextWordtoSearchfor;
while(record != null)
{
//
System.out.println("Good morning, this is SwapnaNaidu. My Hi-Start program finds words in file....");
//For the first string in mydata.txt it is matching/comparing with all the lines in SearchFile.txt and the nextWordtoSearch will be null.
//And for the second value in mydata.txt the file is not looping b'coz nextWordtoSearchfor is already null.
while ((nextWordtoSearchfor = searchFile.readLine()) != null)
{
System.out.println(" Search file content: " + nextWordtoSearchfor);
// We compile pattern to be word we wish to find.
Pattern MyPattern = Pattern.compile(nextWordtoSearchfor, Pattern.UNICODE_CASE);
// Then we get line from file to be examined.
String nextLine = searchFile.readLine(); // [url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine(]BufferedReader (Java 2 Platform SE 5.0)[/url])
//
if(nextLine != null)
{
do
{
//
Matcher matcher = MyPattern.matcher(nextLine);//
if(matcher.find())
{
// This should print the value of nextLine if matcher.find is true,
// we can then break on inner loop - so as to go to next line in file
// to be examined. The work here is the core of the program and should
// be given careful, tediously detailed examination. Later, we can work
// on implementing Boyer-Moore, Horspool, Raita from John Burkard.
resutlsFile.write(matcher.group());// Actually, this is results file - write ( targetFile.readLine() ) Which should already be in String record, that's why I have the break two lines away.
// counters are handy, may be commented out for release.
System.out.println("data file content found: " + matcher.group() + " at line number: " + Integer.toString(++counter));
break;// This should break on inner loop, can be break on lable if any reason to do so is discovered.
}
nextLine = searchFile.readLine();// This has to be place here for my loop construct to have correct sequence of operation...
}
while(nextLine != null);//
}
else
{
// program by testing with if's and while, save exceptions for actual exceptions.
dataFile.close();
searchFile.close();
resultsFile.println("early end encountered....");// may be write line or something other than what I have here.
resultsFile.close();
System.out.println("Encountered blank line in file to be examined...");
return;// bail out, program needs further work.
}
}
// Here we announce program termination.
System.out.println("Program attained normal termination, who says cat's can't dance? They are part of the b'coz .... ");
// close all files as routine program completion.
dataFile.close();
searchFile.close();
resultsFile.println("Normal end of program....");// may be write line or something other than what I have here.
resultsFile.close();
return;// bail out, done.
}
}
else
{
System.out.println("Internal problem with program, contact system administrator.");
}
}
catch(PatternSyntaxException pse)
{
System.out.println("A word in the search file conatins a metacharacter..");
System.out.println(pse.getMessage());;
}
}
}

Hi Nicholas,
thanks for your help.
I am working on unix platform.As I have knowledge on Java,I am trying to do it in Java.
I have the code now in place that can search the word in mydata.txt in the SearchFile.txt and gives the output in results.txt