• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Replacing a word in a text file

 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Sheriff
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edit an existing file
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"?
 
Marshal
Posts: 80133
417
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22818
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sed.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>


 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Whatever you say buddy! And I believe this tiny ad too:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic