Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: to replace a word in file

  1. #1
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default to replace a word in file

    I want to replace "integer" with "int" in a file.Following is the code i have written,but its working.Pls let me know my mistake.
    public static void declare(String[] array) throws IOException {
     
    		// TODO Auto-generated method stub
     
    				BufferedWriter b = new BufferedWriter(new FileWriter("F:\\test\\file.txt",true));
    				StringBuffer s = new StringBuffer();
    				String old="integer",newtxt,newt="int";
    				newtxt=old.replace(old, newt);
    				for(int i=0;i<array.length;i++)
    				{
    				s.append(array[i]).append(" ");
    				}
     
     
    				b.write(s.toString());
    			    b.write(newtxt);
    				b.flush();
    				b.close();
     
     
     
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: to replace a word in file

    What happens when the posted code is executed? Can you post something that shows what it does and add some explanations that say what is wrong and show what you want the output to be.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: to replace a word in file

    The output of the code am getting is
    integer a b int.
    But i want to replace integer with int in output file.

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: to replace a word in file

    What is the text of the original input file?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  5. #5
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: to replace a word in file

    I am writing contents of array to a file.Array has "integer" "a" "b".

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: to replace a word in file

    I don't think the posted code does anything like what you want:
    Where does the currently posted code read the contents of the file?
    Where does the posted code change anything that could have been read from the file?
    Where does the posted code write out the new contents of the file replacing the old contents of the file?
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    shenaz (March 21st, 2013)

  8. #7
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: to replace a word in file

    Why are you doing the replace before the loop? This should be done in the loop on each array piece as it is added to the string. Get array piece, replace words, add to output. Then at the end just write the string. Newtxt doesnt seem to be needed. That should get you started.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  9. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    shenaz (March 21st, 2013)

  10. #8
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: to replace a word in file

    The code i posted is partial.I din post the code of main function where am reading from one file.I stored the read contents in an array and passed it to a function.I have posted only that function.

  11. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: to replace a word in file

    What about my second and third questions?
    If you don't understand my answer, don't ignore it, ask a question.

  12. #10
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: to replace a word in file

    Assuming his array is a list of words in the file then my previous post is valid. Just move where you are replacing old with new into the for loop and have it act on the actual array item (not itself for whatever reason it was).
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  13. #11
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: to replace a word in file

    b.write(s.toString) i used for writing to file. And then b.write(newtxt) for replacing.

    --- Update ---



    --- Update ---

    Thank you.As you told newtxt is not needed.But when i use replace() in for loop it isn't working properly.I used it outside loop instead.It's working.

  14. #12
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: to replace a word in file

    Quote Originally Posted by shenaz View Post
    It's working.
    So are we done here or still having a problem?
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: to replace a word in file

    What's in s and what is in newTxt?

    If s is all of the old file's contents and newTxt is "int"
    how does writing those two values one after the other solve your problem?
    If you don't understand my answer, don't ignore it, ask a question.

  16. #14
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: to replace a word in file

    No problem.I will mark it as solved.

    --- Update ---

    I deleted writing newtxt to file.Instead after for loop i used s.replace() for replacing old contents with new.And then at once i wrote everything to file.

  17. #15
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: to replace a word in file

    Yup, that works. Glad we could help!
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  18. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: to replace a word in file

    What is in the file if you execute the program more than one time?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #17
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: to replace a word in file

    Thank you
    Quote Originally Posted by Chris.Brown.SPE View Post
    Yup, that works. Glad we could help!


    --- Update ---

    File contains algorithm.

  20. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: to replace a word in file

    I guess there are things done in other parts of the program that have not been explained. The posted code appends what it writes to the file. It does not replace the contents of the file.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #19
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: to replace a word in file

    Ah, your concern is that the file is not being wiped prior to output being printed. Otherwise an input file of "this that integer something else" would turn into "this that integer something elsethis that int something else".
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  22. #20
    Member
    Join Date
    Mar 2013
    Posts
    37
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: to replace a word in file

    Ya right.complete code i din't post.it was appending to file but now i changed it.used replace().

    --- Update ---

    Yes yes.So i din use replace() inside loop.

  23. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: to replace a word in file

    it was appending to file but now i changed it.used replace().
    What file I/O class has a replace() method?

    If the program deletes the file BEFORE calling the declare() method, then appending to a new file would work.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. JAVA = Replace a line in a text file
    By JGW in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 16th, 2013, 05:46 AM
  2. how to replace a line in a text file
    By blueblurry_23 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 8th, 2013, 08:12 AM
  3. read a file word by word
    By poornima2806 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2012, 03:14 PM
  4. Reading a text file word by word
    By dylanka in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 21st, 2011, 02:06 PM
  5. Replies: 6
    Last Post: May 25th, 2010, 02:15 AM