File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Replacing a word in a text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Replacing a word in a text file" Watch "Replacing a word in a text file" New topic
Author

Replacing a word in a text file

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

I need to check for a particular word from an existing file and need to replace that word with new word.How should i do this? Now i can able to read that particular word from the existing file.But i dont know how to replace that particular word with new word.

Here is my code to read a particular word:



Please help me to do this.
Thanks>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Edit an existing file


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fredrik Larsback
Greenhorn

Joined: Dec 21, 2009
Posts: 9
Read the file into a StringBuilder, then use String replaceFirst(String regex,String replacement) to replace the word, then write the String back to the file.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thanks for your responses.I have tried using Random access file first using the following code:


But the welcome is inserted at the end of the file.How could i replace the existing word say "Hello world" by "welcome"?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38353
    
  23
Can you replace things in a random access file like that? I thought they would have to be exactly the same length, but I might be mistaken.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
I have tried to replace the value like the following:


Now my output is:

"Hello"></property>
value2: <property name="message" value=hai

But the value is not changed in the xml file.

Thanks>
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Campbell Ritchie wrote:Can you replace things in a random access file like that? I thought they would have to be exactly the same length, but I might be mistaken.

You're not. Although you can shift bytes and if necessary truncate the RandomAccessFile, originally it only allows replacing bytes with the same number of bytes. It shouldn't be used for replacing characters at all because of encodings. That's why I posted the "editing an existing file" FAQ, because that's a better solution.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

sed.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Hi,

I have tried the following code to write the word in the xml file.But this code inserts the line in the file next to the existing line. But how can i delete the existing line?



Actual xml file is bean.xml:

><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/bean...s.xsd">
<bean id="helloWorld" class="com.vaannila.HelloWorld">
<property name="message" value="0"></property>
</bean>

</beans>


the newly created file is bean2.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/bean...s.xsd">
<bean id="helloWorld" class="com.vaannila.HelloWorld">
<property name="message" value="0"></property>
<property name="message" value="1"></property> </bean>

</beans>


How can i delete the old line


<property name="message" value="0"></property>

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
I'd advise to save yourself a lot of headaches and use a proper tool for the job: If you want to alter an XML file, that's what libraries like DOM, JDOM and XOM are for. Using string operations (or regular expressions or something else of that nature) on XML *will break* eventually when the file format changes in a way that the code doesn't foresee.


Ping & DNS - my free Android networking tools app
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Thank you for your advise. To use DOM, should I download any jar file? I am not aware of DOM.Please assist me any samples or link to continue.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
DOM is part of the JAXP API (which is the standard Java API for XML handling), and JAXP is part of the JRE, so you don't need to download or install anything. The XmlFaq contains links to numerous articles that should get you started.

The reason I mentioned other libraries like JDOM and XOM is that the DOM API, while perfectly functional, isn't specifically designed for Java -which JDOM and XOM are- so it is a bit awkward to work with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Replacing a word in a text file