Results 1 to 9 of 9
Thread: Writing to files
- 05-10-2012, 05:23 PM #1
Member
- Join Date
- Dec 2011
- Posts
- 26
- Rep Power
- 0
Writing to files
Hello, how would I go about writing to a .txt file but not rewriting the whole thing. So in other words how can i set it so im just adding text to the text already in the file?
any help would be appreciated!
I want each deposit to be added to the statement.txt file as one full statement off all activity but i just keep rewriting over the file wit one line... Any one care to help me?Java Code:if (depAmount%10==0) { System.out.println("** Please enter your money now **"); TotalBalance = TotalBalance + depAmount; PrintWriter outputFile = new PrintWriter("Statement.txt"); outputFile.println(getDate() + " " + getTime() + " " + "Deposit €" + depAmount); outputFile.close(); } else { System.out.println("\nError - You can only deposit in multiples of 10"); Lodgement(); }Last edited by GoodThing007; 05-10-2012 at 10:10 PM.
- 05-10-2012, 05:32 PM #2
Re: Writing to files
Many writers have a way to append to a file instead of write to it. Check out the API for the class you're using for useful functions and constructors.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 05-10-2012, 10:12 PM #3
Member
- Join Date
- Dec 2011
- Posts
- 26
- Rep Power
- 0
Re: Writing to files
I've added my code to the op so if anybody could please advise me what to change to get it working the way I want it'd help me alot!
-
Re: Writing to files
As per Kevin's excellent suggestion: Create a FileWriter, use the constructor that takes both a file name String and a boolean (check the FileWriter API for the details), and then create a PrintWriter with the FileWriter, passing the FileWriter into the PrintWriter's constructor (rather than a String as you're currently doing). If you do this correctly, your text will be appended to the end of the file. Then when done, you only have to close the PrintWriter, not the FileWriter which will automatically be closed when the PrintWriter closes.
- 05-11-2012, 01:00 AM #5
Member
- Join Date
- Dec 2011
- Posts
- 26
- Rep Power
- 0
Re: Writing to files
I see wat ye'r saying but for some reason I cant do it? can ye give a short example of where its done or even just show me what to change on my own code above?
- 05-11-2012, 01:24 AM #6
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Re: Writing to files
Fubarable have given you a very details instructions on how to do it. Have you tried to translate it into a code? Have you read the FileWriter (Java Platform SE 7 ). There is a constructor in this class that will allow you to append the content of a file.
Any particular thing that make you can not do it?Website: Learn Java by Examples
-
Re: Writing to files
I'd rather have *you* write the code as you will learn more that way.
Have you looked at the constructors in the link I've provided? If not, please look again, and I'll bet you'll figure it out. If not, at least show us what you've tried and we'll help you some more.
- 05-11-2012, 08:02 PM #8
Member
- Join Date
- Dec 2011
- Posts
- 26
- Rep Power
- 0
Re: Writing to files
I managed to pull it together!
This is what you were explaining right?Java Code:FileWriter fw = new FileWriter("Statement.txt", true); PrintWriter outputFile = new PrintWriter(fw); //statement variable below is line passed in and will write to append to statement line without overwriting outputFile.println((getDate() + " " + getTime() + " " + "Deposit €" + depAmount)); outputFile.close();
-
Re: Writing to files
I knew you could!
Yes.This is what you were explaining right?Java Code:FileWriter fw = new FileWriter("Statement.txt", true); PrintWriter outputFile = new PrintWriter(fw); //statement variable below is line passed in and will write to append to statement line without overwriting outputFile.println((getDate() + " " + getTime() + " " + "Deposit €" + depAmount)); outputFile.close();
Similar Threads
-
Writing zip files results in a zip file with a size, but no files when i open it
By Jaeela in forum New To JavaReplies: 11Last Post: 12-04-2011, 10:10 PM -
Error with Writing Files, don't know why??
By teekei in forum New To JavaReplies: 11Last Post: 07-19-2011, 12:26 AM -
writing to files from arrays
By xkillswitchx14 in forum New To JavaReplies: 3Last Post: 04-28-2011, 11:11 PM -
writing the files for particular time
By damuammu in forum Advanced JavaReplies: 3Last Post: 03-15-2011, 06:32 PM -
Applets writing to files
By bugger in forum New To JavaReplies: 2Last Post: 11-20-2007, 08:45 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks