BuferedReader的新对象仍然关闭

时间:2017-03-11 07:10:55

标签: java inputstream bufferedreader

我正在尝试创建一个基于不同的东西读取文件的类。但目前的问题是基本的阅读部分。

我可以很好地阅读文件中的所有行。然后,我可以关闭阅读器并使用新的BufferedReader重新分配reader变量(我使用reader.ready()测试了这一点,而不是抛出IOException,如下面的代码所示)。但是,当我从新的BufferedReader读取时,它会抛出IOException(流关闭):

骨架代码如下:

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;
    }

}

输出也低于:

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

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

你的问题很简单:

关闭缓冲的阅读器将关闭基础输入流。

你也需要一个全新的InputStream。

答案 1 :(得分:1)

当&#34; reader.close();&#34;被叫,它关闭了读者和流。

一个简短的解决方案是在类中保留File f的副本,并使用f在reset()方法中重新创建stream和reader。