Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing Files In Java ! 2

Status
Not open for further replies.

Kadirpatel

Programmer
Mar 28, 2001
43
GB
Hi,
My requirement is to parse a file in Java bcoz I have made a template and now I want to add my values in it and save it. So I want to parse the template file in Java. How can I do it?

Thanx.
 
It depends on what you want to parse. If it's in the format of key-value pairs, you can use java.util.Properties.

e.g.
public void doProperties(String myFile)
{
File file = new File(myFile);
Properties prop = new Properties().
prop.load(new FileInputStream(file));

//add new properties, change them or delete them

prop.store(new FileOutputStream(file), "My new list");
}

This is just one way to do it. I hope it's helpful.

Greetings,
Steven.
 
Thanx for your answer. It has solved my purpose. But now there is one more issue that how can I find a string in a file using Java. I know one method of reading whole file in a String Buffer and do the processing but I want directly to process the file. So what is the solution. Please let me know.

Regards.
Kadir.
 
For directly processing a file you can use java.io.RandomAccessFile. This class supports both reading and writing without having to open a separate Reader and Writer. This class also has a readLine function that returns Strings. Perhaps this is a solution for you.

Greetings,
Steven.
 
Yes I think that will surely solve my purpose. Thanx for ur valuable support. Will need help next time also.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top