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 4 of 4

Thread: newLine isn't working

  1. #1
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default newLine isn't working

    Hi, I'm encountering a slight problem where I'm getting an error for out.newLine();
    Here is the error:

    java.io.IOException: Stream closed
    	at java.io.BufferedWriter.ensureOpen(Unknown Source)
    	at java.io.BufferedWriter.write(Unknown Source)
    	at java.io.Writer.write(Unknown Source)
    	at java.io.BufferedWriter.newLine(Unknown Source)
    	at layout.GameClient2.actionPerformed(GameClient2.java:453)
    	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    	at java.awt.Component.processMouseEvent(Unknown Source)
    	at javax.swing.JComponent.processMouseEvent(Unknown Source)
    	at java.awt.Component.processEvent(Unknown Source)
    	at java.awt.Container.processEvent(Unknown Source)
    	at java.awt.Component.dispatchEventImpl(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    	at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
    	at java.awt.Container.dispatchEventImpl(Unknown Source)
    	at java.awt.Window.dispatchEventImpl(Unknown Source)
    	at java.awt.Component.dispatchEvent(Unknown Source)
    	at java.awt.EventQueue.dispatchEvent(Unknown Source)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    	at java.awt.EventDispatchThread.run(Unknown Source)
    The line is, as I mentioned, out.newLine();

    Here is some relevant code:

                              File rfile4 = new File(dir + "temp.txt");
    		          FileWriter writer = new FileWriter(dir + "temp.txt",true);
    		          BufferedWriter out = new BufferedWriter(writer);
                              pointsfile = "";
                              while(linenum > 1)
                              {
                                      out.newLine();
    			                                 out.write(pointsfile); 
    			                                 out.close();
                              }
    Last edited by The_Mexican; January 7th, 2011 at 08:31 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: newLine isn't working

    If you enter the while loop a second time, the stream has already been closed by a previous loop. linenum is not defined, but I presume you want your loop to go more than once. If so, move the out.close outside the while loop.

  3. The Following User Says Thank You to copeg For This Useful Post:

    The_Mexican (January 7th, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: newLine isn't working

    while(linenum > 1)
                              {
                                      out.newLine();
                                                 out.write(pointsfile);
     
                              }
     out.close();
    Wait...copeg already got there first.



    Also, linenum I'm assuming means line number.
    How about also adding a condition to tell it to exit the while loop when you reach a blank line? That way it won't go on forever.
    Last edited by javapenguin; January 7th, 2011 at 10:41 PM.

  5. #4
    Member
    Join Date
    Feb 2010
    Posts
    81
    Thanks
    18
    Thanked 1 Time in 1 Post

    Default Re: newLine isn't working

    Yes copeg solved it. And don't worry penguin, I didn't include all the code, linenum is the number of lines there are before I get to the line that I need to use and at the end of each time it goes through the loop, I make it decrease by 1. Thanks though.

Similar Threads

  1. Complier not working
    By Benderman in forum The Cafe
    Replies: 3
    Last Post: January 9th, 2011, 06:05 PM
  2. Working with threads
    By tccool in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 12th, 2010, 10:21 AM
  3. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  4. Boggle not working?
    By gandalf5166 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 6th, 2010, 10:53 AM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM