find word in txt
Hi.
How can i find word in text file and add some other word in front of the founded, for example:
"text1 text2 text3, text4" and before text1 i want to put <data> and behind text4 </data>, i need to create xml file from txt, and save to xml file.
Thanks Guru.
[305 byte] By [
maciej80a] at [2007-10-2 17:04:29]

Hi,
Please check the following java code. It will help you.
code]import java.io.*;
import java.util.*;
class FileConverter
{
public static void main(String[] args) throws IOException
{
int lineNo = 0;
boolean flag;
String XmlFile = "C:/Documents and Settings/sppmohan/Desktop/xml/";
String TextFile = "C:/Documents and Settings/sppmohan/Desktop/text/";
File Fileone = new File(TextFile);
File Filetwo = new File(XmlFile);
String Allfiles[] = Fileone.list();
for( int index = 0; index< Allfiles.length; index++)
{
flag= true;
FileInputStream textfis = new FileInputStream( TextFile + Allfiles[index] );
System.out.println("Input File:"+TextFile + Allfiles[index] );
BufferedReader textdis = new BufferedReader(new InputStreamReader((InputStream)textfis));
PrintWriter xmlfos= new PrintWriter(new BufferedWriter(new FileWriter(XmlFile+Allfiles[index].substring(0,Allfiles[index].indexOf("."))+".xml")));
String textLine = textdis.readLine();
lineNo = 0;
while( textLine != null )
{
lineNo++;;
textLine = textLine.trim();
if( textLine.indexOf("text1")!= -1)
{
textLine = textLine.replaceAll("text1", "<data> text1");
}
if( textLine.indexOf("text4")!= -1)
{
textLine = textLine.replaceAll("text4", "text4 </data>");
}
xmlfos.write(textLine+"\n");
textLine = textdis.readLine();
}
textfis.close();
xmlfos.flush();
xmlfos.close();
System.out.println("Output File:"+XmlFile + Allfiles[index].substring(0,Allfiles[index].indexOf("."))+".xml" );
}
}
}
[/code]