This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I need help! basically I have a file called "test.txt" The content of the file is as followed: ---------------Test.txt--------- Honda Civic, 4LSXL,red = 2000 Honda Accord, 3HUV, blue = 1999 Nissan Maxima, 5JKLU, green =2001 Acura Integra, JK22L, red = 2003 ---------------------------------- Basically I want to replace "Nissan Maxima, 5JKLU, Green=2001" with "Nissan Altima, 6LRTU, Yellow=1999". Can any one tell me what is the easiest way to do this? I already started to read the file, I need some help! Thanks, David Sundo ------------Code---------------------------- StringBuffer data = new StringBuffer();
I'll guess you want a Swing UI, and we'll work against the text file kinda like it was a single-user database. See if this sings to you:
Doea that makes sense? Does it hint at some unit tests you can write? Responsibilities for different classes? This is a classic "functional decomposition" analysis, not very OO ... yet. [ July 21, 2003: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
??? I have no idea where Swing or a GUI comes into this...
Basically I want to replace...
Some ideas: you may want to look into regular expressions to locate the text you want to replace (I'm assuming you aren't going to hard-code the string into the program). If you aren't going to do any pattern matching, regular expressions may be over-kill, and you may just need the String.indexOf() method. Then there are methods in the String class that will help you extract the sub-strings necessary to rebuild a new string using your string buffer. hth, bear
Yeah, that Swing bit was a leap. If this is the least work you can do to update a file you can start by deciding if you want the whole file in memory or not.
I used to work on an OS called CMS that had a safe file replacement strategy, used in COPY and certain other output that replaced an existing file:
This gives a "least bad" result if anything croaks in mid flight. Yet another approach: You CAN update the file in place. Force all car records to have the same number of bytes. Use RandomAccessFile to seek a position (record number * record length) and read (record length) bytes. If you find the old data, seek the same position again, and write the new bytes. Just for practice with test first coding, I made a random access fixed length file class with the following public methods:
If that sounds like fun, I'd be happy to share it. Or maybe just the Junit test first.