Results 1 to 5 of 5
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Purpose of close() ?

    Hi.

    My Code is:

    Java Code:
    public class TestClass {
    	
    	public static void main(String[] args) {
    		
    		PrintWriter outputStream = null;
    		
    		try
    		{
    			outputStream = new PrintWriter(new FileOutputStream("c:/stuff.txt"));
    		}
    		catch(FileNotFoundException e)
    		{
    		}
    		
    		System.out.println("Writing to file.");
    		
    		for(int i = 0;i<10000;i++)
    		{
    			outputStream.println(i);
    		}
    		
    		//outputStream.close();
    	}
    }
    So my question is:

    How does JAVA decide on when to call .close?

    When outputStream.close is commented out:

    When I put i<100 for example, there are no values in my stuff.txt file.
    When I put a larger number, such as 10000, I see some values in my stuff.txt file, up to ~8500 for example.

    If I do not comment out close(), then whatever the number i is, I get correct amount of integers in my stuff.txt

    Any ideas?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Purpose of close() ?

    If you do not call close, data could be left in a buffer waiting for more data to fill the buffer before doing a write to disk.
    Its best to close the file as soon as the writing or reading is done.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    4

    Default Re: Purpose of close() ?

    Thanks.

    But how does it decide when to do that ?

    And in a Book called Absolute Java (3rd Edition) it says close is always called automatically when program finishes. It doesn't look so true?

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default Re: Purpose of close() ?

    Its better if you do it, rather than hope the JVM will do it.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Purpose of close() ?

    close() calls flush() before closing the stream (in most stream cases).
    flush() sends the contents of the buffer to wherever it is supposed to go (disk in your case).

    And that book is wrong. There is no guarantee that a stream will close as a program exits.
    You should do so explicitly, and in a finally block.

Similar Threads

  1. interface purpose?
    By frejon26 in forum New To Java
    Replies: 5
    Last Post: 06-27-2014, 10:41 AM
  2. Can't understand the purpose of interfaces
    By Vortexnl in forum New To Java
    Replies: 3
    Last Post: 02-13-2011, 06:10 PM
  3. Connecting two or more pcs for chating purpose
    By Hussain Ali in forum Networking
    Replies: 2
    Last Post: 10-21-2010, 06:30 AM
  4. Replies: 13
    Last Post: 04-09-2010, 12:15 PM
  5. Over-riding purpose fails..
    By udayadas in forum New To Java
    Replies: 7
    Last Post: 08-24-2008, 05:14 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
  •