Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: question about default of outFile()

  1. #1
    Junior Member
    Join Date
    Aug 2010
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default question about default of outFile()

    hey guys, how do i change the default of throws FileNotFoundException so that it would not automatically erase the contents in my desired output file.

    my aim is to inFile() and outFile() the contents in the same location.


    this:
    Scanner inFile=new Scanner(new FileReader("vending_machine"));
    PrintWriter outFile=new PrintWriter("vending_machine");

    my problem is the contents in the vending_machine text file automatically goes blank right after i run the program. So how should i change the default and change it to not automatically erasing the contents of the text file. Thanks!

    Sorry for my horrible english and grammar. haha
    Last edited by helloworld922; August 18th, 2010 at 12:07 PM.


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: question about default of outFile()

    Ok, well my guess is that it isnt that the text file goes blank, but rather that the exception is thrown prior to completing the writing or the closing. Without code to see, I wont really know what is going on.

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: question about default of outFile()

    I believe if you create your PrintWriter from a FileOutputStream with the append option on, it should append everything you write at the end of the file.

    PrintWriter outFile = new PrintWriter(new FileOutputStream("vending_machine", true));

    As a side-note, I don't think you should have the same file open for reading/writing at the same time. Instead, you should close/re-open the stream between reads/writes.

  4. The Following User Says Thank You to helloworld922 For This Useful Post:

    odine (August 18th, 2010)

Similar Threads

  1. Replies: 5
    Last Post: August 5th, 2010, 09:16 PM
  2. get proxy settings from the default system browser
    By reguapo in forum Java Networking
    Replies: 0
    Last Post: January 15th, 2010, 08:43 AM
  3. Default Access (package access) confusion
    By gauravrajbehl in forum Java Theory & Questions
    Replies: 1
    Last Post: November 18th, 2009, 04:11 AM