Results 1 to 2 of 2
  1. #1
    Sitz is offline Member
    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Unhappy Open a .txt file and add to it. HELP!

    Java Code:
    FileOutputStream fout;
    				
    				try
    				{
    					System.out.println ("\nPlease enter the name of the book:") ;
    					name = keyboard.next() ;
    					
    					System.out.println ("\nPlease enter the author of the book:") ;
    					author = keyboard.next() ;
    					
    					System.out.println ("\nPlease enter the ISBN of the book:") ;
    					isbn = keyboard.next() ;
    					
    					System.out.println ("\nPlease enter the year of the book:") ;
    					year = keyboard.next() ;
    					
    					fout = new FileOutputStream ("db.txt") ;
    					new PrintStream(fout).println (name + ", " + author + ", " + isbn + ", " + year + ".") ;
    					fout.close() ;
    				}
    				
    				catch (IOException e)
    				{
    					System.err.println ("Unable to write to file") ;
    					System.exit(-1) ;
    				}
    This is my code. Each time I run the program, all it does is create a new "db.txt" file and overwrites the previous one, and all the information is deleted and the new one is entered.
    I want to know how to just change line (\n or something) and add to the db.txt the new data.

    Thanks in advance.
    Last edited by Sitz; 03-15-2012 at 04:31 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,751
    Rep Power
    19

    Default Re: Open a .txt file and add to it. HELP!

    Please use [code] tags [/code] when posting code.

    Have a look at the FileOutputStream API. There's a constructor there that tells the stream to do what you want it to.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 15
    Last Post: 02-16-2012, 03:43 PM
  2. I/O How to open a file
    By sehudson in forum New To Java
    Replies: 8
    Last Post: 07-18-2011, 03:32 AM
  3. Replies: 0
    Last Post: 03-05-2010, 04:21 AM
  4. Different Files Open in File -> Open Workspace
    By mgm2010 in forum JCreator
    Replies: 0
    Last Post: 04-11-2009, 02:14 PM
  5. To open an image file such as Jpeg file using JAva Program
    By itmani2020 in forum Advanced Java
    Replies: 10
    Last Post: 07-11-2008, 09:57 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
  •