Results 1 to 7 of 7
  1. #1
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Default Buffered Reader Exception

    I cant understand where my program has gone wrong. It produces this error
    deps-jar:
    compile:
    run:
    IO Error! Stream closed
    java.io.IOException: Stream closed
    at java.io.BufferedReader.ensureOpen(BufferedReader.j ava:97)
    at java.io.BufferedReader.readLine(BufferedReader.jav a:292)
    at java.io.BufferedReader.readLine(BufferedReader.jav a:362)
    at lab8.main(lab8.java:33)
    BUILD SUCCESSFUL (total time: 0 seconds)

    Here is the source code

    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */



    import java.util.Scanner;
    import java.io.*;


    public static void main(String[] args) {
    try{
    Scanner sc = new Scanner (System.in);

    //Step 1: create and open file stream
    FileReader openfile = new FileReader ("names.txt");
    BufferedReader brstream = new BufferedReader (openfile);

    //Step 2 : must write into new file
    FileWriter fw1= new FileWriter ("small.txt");
    BufferedWriter bw1 = new BufferedWriter (fw1);
    PrintWriter pw1 = new PrintWriter (bw1);

    FileWriter fw2= new FileWriter ("big.txt");
    BufferedWriter bw2 = new BufferedWriter (fw2);
    PrintWriter pw2 = new PrintWriter (bw2);

    //Step 3: check length of name
    String name = brstream.readLine();
    while ( (brstream.readLine() != null) ){
    if (name.length() < 5)
    {pw1.println(name);}
    else { pw2.println(name) ; }

    //step 4: close all the stream
    brstream.close();
    pw1.close();
    pw2.close(); } //close while
    } //close try

    catch(FileNotFoundException e)
    {System.out.println("File not found!"+e.getMessage() );
    System.exit(0);}

    catch(IOException e)
    {System.out.println("IO Error! "+ e.getMessage());
    e.printStackTrace();
    System.exit(0);}

    }
    }

    Basically this program reads a text file "names.txt". There is one name in each line of the file with no whitespace characters. If the length of the name is less than 5, it is printed in "small.txt" else it is printed in "big.txt". Can anybody tell me what is wrong with my program?

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Your close() statements are in the while loop. I think they should after your while loop.
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    hitmen is offline Member
    Join Date
    Oct 2008
    Posts
    58
    Rep Power
    0

    Red face

    I have already changed my program so that the pwstream is b4 the while loop. However, how my program reads only alternative names from my name.txt. Thereafter, it reads the null string and then produce an error message. Here it is

    Name is Beckham
    Name is Blanc
    Name is Scholls
    Name is McDonald
    Name is BurgerKing
    Name is PHut
    Name is null
    Exception in thread "main" java.lang.NullPointerException
    at lab8.main(lab8.java:36)

    Here is my original text file ("names.txt"):

    Alex
    Beckham
    Giggs
    Blanc
    Neville
    Scholls
    Keane
    McDonald
    KennyRoger
    BurgerKing
    AandW
    PHut

    and here is my new program:

    import java.util.Scanner;
    import java.io.*;

    public class lab8 {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    try{
    Scanner sc = new Scanner (System.in);
    //Step 1: create and open file stream
    FileReader openfile = new FileReader ("names.txt");
    BufferedReader brstream = new BufferedReader (openfile);
    //Step 2 : must write into new file
    FileWriter fw1= new FileWriter ("small.txt");
    BufferedWriter bw1 = new BufferedWriter (fw1);
    PrintWriter pw1 = new PrintWriter (bw1);

    FileWriter fw2= new FileWriter ("big.txt");
    BufferedWriter bw2 = new BufferedWriter (fw2);
    PrintWriter pw2 = new PrintWriter (bw2);

    //Step 3: check length of name
    String name;
    while ( (brstream.readLine() != null) ){
    name = brstream.readLine();
    System.out.println("Name is " + name);
    if (name.length() < 5){pw1.println(name);}
    else { pw2.println(name) ; }
    }//close while

    //step 4: close all the stream
    brstream.close();
    pw1.close();
    pw2.close();}//close try

    catch(FileNotFoundException e)
    {System.out.println("File not found!"+e.getMessage() );
    System.exit(0); }

    catch(IOException e)
    {System.out.println("IO Error! "+ e.getMessage());
    e.printStackTrace();
    System.exit(0);}

    }
    }

    Thanks!

  4. #4
    parulrana9 is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Default

    Hi I am facing a similar problem. The bufferdReader readLine() method seams to read alternate lines from the file. Can anyone explain why is it so?

    public class ReadFile {

    public static void main(String[] args){
    File file= new File("c:\\b.txt");
    try{
    FileReader fileReader = new FileReader(file);
    BufferedReader br = new BufferedReader(fileReader);
    String line = "";
    while((line = br.readLine()) != null){
    System.out.println(br.readLine());
    }

    }
    catch(Exception e){
    e.printStackTrace();
    }
    }
    Last edited by parulrana9; 01-07-2009 at 07:54 AM.

  5. #5
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    Well... do you see the bit where you're calling readLine() twice but only printing once...?

  6. #6
    parulrana9 is offline Member
    Join Date
    Jan 2009
    Posts
    2
    Rep Power
    0

    Red face

    Thanks a lot for the reply. It was that easy!

  7. #7
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    @hitman
    Java Code:
    while (brstream.readLine() != null) {
       name = brstream.readLine();
       System.out.println("Name is " + name);
       if (name.length() < 5)
          pw1.println(name);
       else
          pw2.println(name);
    }
    You made the same mistake neilcoffey found in parulrana9's code:
    Quote Originally Posted by neilcoffey
    Well... do you see the bit where you're calling readLine() twice but only printing once...?
    And, please, USE CODE TAGS to wrap your code... It's quite difficult for us to read...

Similar Threads

  1. Unable to draw buffered image
    By pedjasmek in forum Java 2D
    Replies: 7
    Last Post: 08-08-2008, 04:49 PM
  2. FileReader / Buffered Reader
    By sepaht in forum New To Java
    Replies: 9
    Last Post: 07-10-2008, 09:05 PM
  3. A simple DOM reader
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-03-2008, 10:24 AM
  4. help with file reader
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:03 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
  •