0

This code gives me an error when I take more than one data by keyboard in the same application, it gives an IOException error;

gives error when leaving the do while

I don't know why he makes this kind of mistake.

 public static String datoString() {
// Entorno:
    BufferedReader br;

    String frase;
    boolean esCorrecto;
    //Algoritmo
    frase=null;
    br = new BufferedReader(new InputStreamReader(System.in));
    try {
        do {

            System.out.println("Introduce una cadena");
            frase = br.readLine();
            esCorrecto = true;


        } while (!esCorrecto);

    } catch (IOException ioe) {
        System.err.println("Error I/O");
    }
    try {
        br.close();
    } catch (IOException ioe2) {
        ioe2.printStackTrace();
    }//Fin try


    return frase;

}
6
  • 1
    What errors ??? Commented Apr 28, 2018 at 16:50
  • 1
    Why are you printing one stacktrace but ignoring the other?? Print them all Commented Apr 28, 2018 at 16:50
  • The stack trace would likely tell you the issue. Commented Apr 28, 2018 at 16:50
  • @AndyTurner: not the way he's got it wired Commented Apr 28, 2018 at 16:51
  • 1
    When you called br.close(); you can no longer use System.in in another call to datoString() Commented Apr 28, 2018 at 16:52

1 Answer 1

2

By doing this

 br.close();

You are actually doing this

System.in.close();

Because BufferedReader closes underlying stream.

This makes System.in stream no longer available for use. What you need to do, is to do a little trick to prevend System.in from closing. To do that, you can use following wrapper

public class ShieldedInputStream extends InputStream {

    InputStream source;

    public ShieldedInputStream(InputStream source) {
        this.source = source;
    }

    @Override
    public int read() throws IOException {
        return source.read();
    }

    @Override
    public int read(byte[] b) throws IOException {
        return source.read(b);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        return source.read(b, off, len);
    }

    @Override
    public long skip(long n) throws IOException {
        return source.skip(n);
    }

    @Override
    public int available() throws IOException {
        return source.available();
    }

    @Override
    public void close() throws IOException {
//        source.close(); // We dont awant to close it!
    }

    @Override
    public void mark(int readlimit) {
        source.mark(readlimit);
    }

    @Override
    public void reset() throws IOException {
        source.reset();
    }

    @Override
    public boolean markSupported() {
        return source.markSupported();
    }
}

And use it like this

br = new BufferedReader(new InputStreamReader(new ShieldedInputStream(System.in)));

This way you will prevent System.in from closing, but still allowing you to free resources by closing BufferedReader

Sign up to request clarification or add additional context in comments.

1 Comment

To make your code shorter, you can extend FilterInputStream.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.