| Author |
Writing to files question
|
Caleb Ash
Greenhorn
Joined: Dec 29, 2010
Posts: 5
|
|
Okay, I have 4 files...
I really don't know how to do this,
I've been reading over my book "Blue Pelican Java",
on the section for appending but it doesn't make sense to me,
or at least I can't think of how to apply in the way I want.
I really only need 2 examples, one for the actNum situation,
and one for the other 3(as they use the same method).
I would be very thankful, and I know this isn't a code mill,but I cannot
figure this out
actNum.in | actPass.in | actIdnt.in | actAmt.in
0............ |.. 123245.. |.. Caleb A | 50.45
Each one has information on corresponding lines, as above
What code would i need to be able to produce the following things on a file(The ....'s are for spacing)
actNum would be automatic, 0 - what ever;
actPass on use input, same for actIdnt and actAmt;
actNum.in | actPass.in | actIdnt.in | actAmt.in
0............ | 123245 | Caleb A | 50.45
1............ | 846287 | Ryan Z | 134.21
|
 |
Adeiza Yusuf
Greenhorn
Joined: Oct 06, 2010
Posts: 24
|
|
Hello, If I understand you correctly, you can make your method this simple and any time you call the method with the user information a space is given before the next is added.
|
 |
Caleb Ash
Greenhorn
Joined: Dec 29, 2010
Posts: 5
|
|
editting it a little bit:
So, I would use something like
addIn(pass.nextString(), actPass.in);
Correct?
Also, any idea about the actNum situation?
Where it would be the line number (Seeing as files counts the same as an array, first line is 0, 2nd line is 1)
|
 |
Adeiza Yusuf
Greenhorn
Joined: Oct 06, 2010
Posts: 24
|
|
|
for the numbers you can use an int variable that you increment by 1 every time the file is written
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32712
|
|
You would do well to find out about BufferedWriters, which you will read about in the Java™ Tutorials. Remember to use a finally block to close your writer. You can write a whole line at once with a BufferedWriter.
Decide whether you wish to handle the IOException in your writing method.
It may be inefficient to call the method to write a single line, because you are repeatedly creating and deleting object references. See whether you can get the writing into a while loop. Again you will find similar examples in that Java™ Tutorials link I gave you.
|
 |
 |
|
|
subject: Writing to files question
|
|
|