Search code examples
javainputstreambufferedreader

New object of BuferedReader is still apparantly closed


I am trying to create a class that reads a file based off different things. But the problem for the moment is the fundamental reading parts.

I can read all the lines in a file fine. I can then close the reader and reassign the reader variable with a new BufferedReader (I tested this with reader.ready() not throwing an IOException, as you can see in the code below). However, when I read from the new BufferedReader, it throws a IOException (stream closed):

The skeleton code is below:

SeparatorReader.java

package SP.Reading;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class SeparatorReader{

    private InputStream stream;
    private BufferedReader reader;

    public SeparatorReader(File f) throws FileNotFoundException{
        stream = new FileInputStream(f);
        reader = new BufferedReader(new InputStreamReader(stream));
    }

    public synchronized String readLine() throws IOException{
        return reader.readLine();
    }

    public synchronized List<String> readAllLines() throws IOException{
        List<String> ls = new ArrayList<String>();
        String line;
        while((line = reader.readLine())!=null){
            ls.add(line);
        }
        return ls;
    }

    public synchronized void reset() throws IOException {
        reader.close();
        reader = new BufferedReader(new InputStreamReader(stream));
        System.out.println(reader.ready());
    }

}

ReadingTester.java

package SP.Reading;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class ReadingTester {

    private static boolean error = false;
    private static boolean error811 = false;
    private static boolean error812 = false;

    public static void main(String[] args) {
        File f = buildFile();
        try {
            SeparatorReader fileReader = new SeparatorReader(f);
            System.out.println("Commencing test 1 of 27: Reading all the lines from a file");
            List<String> lines = fileReader.readAllLines();
            fileReader.reset();
            if(lines.get(0).equals("line 1") && lines.get(1).equals("line 2") && lines.get(2).equals("line 3")) System.out.println("Reading all the lines from a file complete!");
            else{
                error = true;
                error811 = true;
                System.out.println("Unable to read all the lines from a file");
            }

            System.out.println("Commencing test 2 of 27: Reading the first line from a file");
            String l = fileReader.readLine();
            fileReader.reset();
            if(l == null) System.out.println("l is null!");
            if(l.equals("line 1")) System.out.println("Reading the first line from a file complete!");
            else{
                error = true;
                error812 = true;
                System.out.println("Unable to read the first line from a file");
            }
        }catch(IOException e){
            e.printStackTrace();
        }

        if(!error) System.out.println("No errors");
        else{
            if(error811) System.out.println("811");
            if(error812) System.out.println("812");
        }
    }

    private static File buildFile(){
        File f = new File("SeparatorReaderFile.txt");
        try {
            f.createNewFile();
            BufferedWriter writer = new BufferedWriter(new FileWriter(f));
            writer.write("line 1");
            writer.newLine();
            writer.write("line 2");
            writer.newLine();
            writer.write("line 3");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return f;
    }

}

The output is also below:

Commencing test 1 of 27: Reading all the lines from a file
false
Reading all the lines from a file complete!
Commencing test 2 of 27: Reading the first line from a file
java.io.IOException: Stream Closed
        at java.io.FileInputStream.readBytes(Native Method)
        at java.io.FileInputStream.read(Unknown Source)
        at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at SP.Reading.SeparatorReader.readLine(SeparatorReader.java:24)
        at SP.Reading.ReadingTester.main(ReadingTester.java:30)
No errors

Thanks in advance for all your help!


Solution

  • Your problem is simple:

    Closing your buffered reader will close the underlying input stream.

    You need a completely new InputStream here as well.