Results 1 to 9 of 9
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By GoodThing007

Thread: Writing to files

  1. #1
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default 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!

    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();
    		}
    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?
    Last edited by GoodThing007; 05-10-2012 at 11:10 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,016
    Rep Power
    10

    Default 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.
    Fubarable likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default 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!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  5. #5
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default 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?

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default 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?

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing to files

    Quote Originally Posted by GoodThing007 View Post
    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?
    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.

  8. #8
    GoodThing007 is offline Member
    Join Date
    Dec 2011
    Posts
    34
    Rep Power
    0

    Default Re: Writing to files

    I managed to pull it together!
    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();
    This is what you were explaining right?
    Fubarable likes this.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Writing to files

    Quote Originally Posted by GoodThing007 View Post
    I managed to pull it together!
    I knew you could!

    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();
    This is what you were explaining right?
    Yes.

Similar Threads

  1. Replies: 11
    Last Post: 12-04-2011, 11:10 PM
  2. Error with Writing Files, don't know why??
    By teekei in forum New To Java
    Replies: 11
    Last Post: 07-19-2011, 01:26 AM
  3. writing to files from arrays
    By xkillswitchx14 in forum New To Java
    Replies: 3
    Last Post: 04-29-2011, 12:11 AM
  4. writing the files for particular time
    By damuammu in forum Advanced Java
    Replies: 3
    Last Post: 03-15-2011, 07:32 PM
  5. Applets writing to files
    By bugger in forum New To Java
    Replies: 2
    Last Post: 11-20-2007, 09:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •