Results 1 to 3 of 3

Thread: changing some words in a text file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,364

    changing some words in a text file

    i have some textfiles with essay in them. i want to change all the words 'dogs' to 'cats' automatically by code

    anyone know how i could do this in java?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: changing some words in a text file

    read the file into a string (fully or buffered) use the String.replace method then write the string back to the file
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Frenzied Member oceanebelle's Avatar
    Join Date
    Jun 2005
    Location
    my n00k.
    Posts
    1,064

    Re: changing some words in a text file

    Hi,

    Just like what ComputerJy said, read the file using BufferedInputStream for buffered.

    The way I would work with it is, read the file line per line. For each line replace the text to replace and then write it out to a temp file. When all lines are read and written, delete the original file and rename the temp into the original file.

    lots of approach really. not sure if mine is the fastest you'll have to do a bit more research.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width