Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsDiscussion Forum → java-ish question on file writing
java-ish question on file writing
2007-06-21, 8:41 AM #1
So I'm writing the file writing part of a program now. I'm doing some research, and I can't find a good way of writing to the file.

I need to write to different lines in the file, not always at the end, which is what most examples are.

The current/temporary plan is to read the whole file into a hashmap, and then update the hashmap, and when the person is done tagging, rewrite the database file from the hashmap.

I'm suuure there is a better solution, and I'll keep looking. Just wondering if anyone knew of a way to write to certain lines using some sort of writer.

(Basically, I have a file containing a lot of data. I need to append data onto different lines, depending on the data. I'm not sure how to tell any of the writer objects to write on line x.)
My Parkour blog
My Twitter. Follow me!
2007-06-21, 8:56 AM #2
This might be helpful or totally useless, I dunno. In PHP, you'd have to manually control the file pointer. You'd place the pointer wherever you want to write to (after reading through the file to find the specific line you're looking for), and write.
2007-06-21, 9:01 AM #3
Yeah, not helpful at all. :p Java doesn't really use pointers (at least not for the stuff I've had to do.)

I did find a way though:

Basically, read each line and check to see if it contains the string I want. If it does, do something to it. If it doesn't, just return the string. Read it into an arraylist, rinse, wipe hands on pants, repeat.

And then at the end, rewrite it to the file. Not fantastic, and kind of what I was going to do before, but I guess it'll work.

Link to post I found: http://www.javaworld.com/javaforums/showflat.php?Cat=2&Number=46255&an=0&page=7
My Parkour blog
My Twitter. Follow me!
2007-06-21, 9:57 AM #4
If you're doing something similar to what the guy in your link wanted to do, I suggest looking into a serialized StringTokenizer. Are you familiar with serialization? It makes file-writing and -reading a snap.
"it is time to get a credit card to complete my financial independance" — Tibby, Aug. 2009
2007-06-21, 10:17 AM #5
file pointers are not the same thing as memory pointers.

it's essentially an index into the file and I guarantee to you that java lets you manipulate it.
2007-06-21, 10:29 AM #6
Freelancer - I'll look into StringTokenizer later.

And Jon'C- Thanks, I didn't know that.


I just implemented a similar version of what was in the link, and it works. So horrah.

END OF THREAD. I guess.
My Parkour blog
My Twitter. Follow me!
2007-06-21, 10:35 AM #7
NOPE! The thread must go on!

:rolleyes::tinfoil::rolleyes:

↑ Up to the top!