Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-12-2008, 09:23 PM
Member
 
Join Date: Apr 2008
Posts: 2
ninaT is on a distinguished road
[SOLVED] Help...
Hi, i hope someone could help me...

I just want to look in a file for an specific word and when I find it i want to replace that word with some other.

Any idea on how could i do that? and Can I do that with any type of file,( .txt, .properties, etc)???

Thanks in advanced
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-13-2008, 12:08 AM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
Try this and let me know if it is what you were looking for. It will accept any filetype and will overwrite the existing file with the new one. I didnt bother testing it much, but it worked with what i had except in notepad the new lines didnt actually make a new line but it looked ok in wordpad and when used as more input.

Code:
static void replaceWord(String filename, String oldword, String newword) { String temp=""; String line; try { FileReader input = new FileReader(filename); BufferedReader bufRead = new BufferedReader(input); while ((line=bufRead.readLine())!=null) { temp += line+"\n"; } temp=temp.replace(oldword, newword); System.out.println(temp); input.close(); bufRead = null; input = null; FileWriter fstream = new FileWriter(filename); BufferedWriter out = new BufferedWriter(fstream); out.write(temp); out.close(); } catch (Exception e) { System.out.println("error"); e.printStackTrace(); } }

Last edited by Chris.Brown.SPE : 04-13-2008 at 12:11 AM.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-15-2008, 10:22 PM
Member
 
Join Date: Apr 2008
Posts: 2
ninaT is on a distinguished road
Thanks!!! everything worked great!!!!!
Gracias!!!
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-15-2008, 10:49 PM
Chris.Brown.SPE's Avatar
Member
 
Join Date: Apr 2008
Location: State College, PA
Posts: 50
Chris.Brown.SPE is on a distinguished road
Send a message via AIM to Chris.Brown.SPE
Be sure to mark the thread as solved. And click the rep button for me if you got time. Thanks. Glad i could help.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 10:43 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org