Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Replacing a word in a text file

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Edit an existing file
 
Fredrik Larsback
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48415
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sed.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic