• 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
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

reading a file, then replacing a string...

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
heres the package (compiles great)
http://www.outrun.org/shiz/skool/Students.java



heres the main java file (works and compiles fine)
http://www.outrun.org/shiz/skool/part2.java



Okay, its supposed to create a text.txt file, and it does, then with each student enter in a new entry of student in the text file. which it does fine.

It also can display all the contests of the text file, which is does fine.

however I am extremely stuck at how to get it to search the text file for a certain word, and rplace it with another word. I know I'm going to have to throw the entire text file into an instance of a string, but how (syntactically) would I do that? and how would I make it search and replace the text? I think I have to use the pushback somehow.. right?

any help is appreciated, thanks
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your replacement value is exactly the same length as your find value, you can get real tricky with RandomAccessFile and update it in place. This doesn't often come together unless you have carefully designed fixed length records in your file.

So, much more likely you'll have to read the file, replace the value in memory and rewrite the file. You can read the whole thing into one string and do replacements, or read, replace and write one line at a time. If you're in JDK 1.4 (or want to use a 3rd party jar) you can look at Pattern for regular expressions, otherwise look at String.indexOf() for searching and lots of String.substring() for replacing.

Here's a trick I picked up from an old IBM operating system ...

That's a very paranoid and safe way to avoid losing the file through some error.

Holler if you need guidance on the particulars.
[ November 16, 2004: Message edited by: Stan James ]
 
If you settle for what they are giving you, you deserve what you get. Fight for this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic