Results 1 to 10 of 10
  1. #1
    ChrisChan is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Post HELP: Exception in thread "main" java.io.IOException: Stream closed

    Hi Guys, i'm new in java programming can someone help me to fix this exception,but whenever i compile my program it has no errors, the error always happen after the loop saying

    Exception in thread "main" java.io.IOException: Stream closed
    at sun.nio.cs.StreamDecoder.ensureOpen(StreamDecoder. java:46)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:1 47)
    at java.io.InputStreamReader.read(InputStreamReader.j ava:184)
    at java.io.BufferedReader.fill(BufferedReader.java:15 4)
    at java.io.BufferedReader.readLine(BufferedReader.jav a:317)
    at java.io.BufferedReader.readLine(BufferedReader.jav a:382)
    at StudentInfo2.main(StudentInfo2.java:26)


    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    public class StudentInfo2
    {
    	public static void main(String[] args)throws IOException, FileNotFoundException
    	{
    		Scanner scan = new Scanner(System.in);
    		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    		String input = "", studN="", add="", contact="";
    		int choose;
    		char buffer1[] = new char[input.length()];
    		char buffer2[] = new char[studN.length()];
    		char buffer3[] = new char[add.length()];
    		char buffer4[] = new char[contact.length()];
    		FileWriter fw = new FileWriter("file1.txt");
    
    		do
    		{
    		System.out.println("Choose (1): Record Data \n       (2): View Data \n       (3): EXIT");
    		choose = scan.nextInt();
    
    		if(choose==1)
    		{
    			System.out.print("Student No: ");
    			studN = br.readLine();
    			System.out.print("Name: ");
    			input = br.readLine();
    			System.out.print("Address: ");
    			add = br.readLine();
    			System.out.print("Contact #: ");
    			contact = br.readLine();
    			System.out.println();
    
    		for(int i=0; i<=buffer1.length; i+=1)
    		{
    			fw.write(buffer1);
    			fw.write(buffer2);
    			fw.write(buffer3);
    			fw.write(buffer4);
    		}
    
    		fw.close();
    		{
    			FileWriter f2 = new FileWriter("C:/Users/MAX BALLENA/desktop/stud_info.txt");
    			f2.write("\r\nStudent Number: "+studN);
    			f2.write("\r\nName: "+input);
    			f2.write("\r\nAddress: "+add);
    			f2.write("\r\nContact Number: "+contact);
    			f2.close();
    		}
    		}
    			else if (choose==2)
    			{
    				FileReader fr = new FileReader("C:/Users/MAX BALLENA/Desktop/stud_info.txt");
    				br = new BufferedReader(fr);
    			while((input = br.readLine())!= null)
    			{
    				System.out.println(input);
    			}
    				fr.close();
    			}
    
    		}
    		while(choose !=3);
    
    	}
    }
    Thanks In Advance :D
    Last edited by ChrisChan; 01-30-2013 at 05:11 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: HELP: Exception in thread "main" java.io.IOException: Stream closed

    When posting code, please use the code tags to preserve formatting.

    When do you close the stream?
    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
    ChrisChan is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: HELP: Exception in thread "main" java.io.IOException: Stream closed

    Quote Originally Posted by KevinWorkman View Post
    When posting code, please use the code tags to preserve formatting.

    When do you close the stream?
    after writing, viewing then reading the text file

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: HELP: Exception in thread "main" java.io.IOException: Stream closed

    Are you sure? That error seems to suggest otherwise. I can't really read unformatted code though, so the best I can do is suggest you step through this with a debugger, or at least add some print statements to figure out what's going on.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: HELP: Exception in thread "main" java.io.IOException: Stream closed

    You can edit your original post to enclose the code in [code] tags [/code].
    That way we might actually be able to read it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    ChrisChan is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: HELP: Exception in thread "main" java.io.IOException: Stream closed

    Quote Originally Posted by Tolls View Post
    You can edit your original post to enclose the code in [code] tags [/code].
    That way we might actually be able to read it.
    how can i do that? sorry i'm just new here in forum :D

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: HELP: Exception in thread "main" java.io.IOException: Stream closed

    Quote Originally Posted by ChrisChan View Post
    how can i do that? sorry i'm just new here in forum :D
    Then you should have read the FAQ before posting, which contains information on how to do all of these things:
    Reading and Posting Messages
    BB Code List - Java Programming Forum - Learn Java Programming
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Default Re: HELP: Exception in thread "main" java.io.IOException: Stream closed

    Quote Originally Posted by ChrisChan View Post
    how can i do that? sorry i'm just new here in forum :D
    There's an edit post button attached to the post...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    ChrisChan is offline Member
    Join Date
    Jan 2013
    Posts
    6
    Rep Power
    0

    Default Re: HELP: Exception in thread "main" java.io.IOException: Stream closed

    Quote Originally Posted by KevinWorkman View Post
    Then you should have read the FAQ before posting, which contains information on how to do all of these things:
    Reading and Posting Messages
    BB Code List - Java Programming Forum - Learn Java Programming
    i've already fixed it

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

    Default Re: HELP: Exception in thread "main" java.io.IOException: Stream closed

    Can you provide a run through that gives the error?
    That is what the user inputs?
    I can see at least one route through where the stream used by 'br' is closed and the loop goes back round with a closed reader (user selects '2' then '1').
    It's hard to tell because your indentation is all over the place.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 1
    Last Post: 11-02-2012, 11:38 PM
  2. Replies: 2
    Last Post: 06-20-2012, 12:18 AM
  3. Replies: 5
    Last Post: 12-21-2011, 06:44 PM
  4. Replies: 14
    Last Post: 04-08-2011, 06:01 PM
  5. Replies: 5
    Last Post: 06-22-2010, 03:51 PM

Tags for this Thread

Posting Permissions

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