java - Close Scanner without closing System.in -
i'm trying re-factor big , used part of application separate methods create easier maintain.
some of these methods asks user input , input validation, i've used scanner , system.in when close scanner close system.in
so question is, can prevent system.in beingness closed shielding closeshieldinputstream or should start passing scanner methods?
you can ignore close implementing custom decorator.
public class unclosabledecorator extends inputstream { private final inputstream inputstream; public unclosabledecorator(inputstream inputstream) { this.inputstream = inputstream; } @override public int read() throws ioexception { homecoming inputstream.read(); } @override public int read(byte[] b) throws ioexception { homecoming inputstream.read(b); } @override public int read(byte[] b, int off, int len) throws ioexception { homecoming inputstream.read(b, off, len); } @override public long skip(long n) throws ioexception { homecoming inputstream.skip(n); } @override public int available() throws ioexception { homecoming inputstream.available(); } @override public synchronized void mark(int readlimit) { inputstream.mark(readlimit); } @override public synchronized void reset() throws ioexception { inputstream.reset(); } @override public boolean marksupported() { homecoming inputstream.marksupported(); } @override public void close() throws ioexception { //do nil } }
and utilize in main
public static void main(string[] args) throws exception { system.setin(new unclosabledecorator(system.in)); }
java design java.util.scanner system.in
No comments:
Post a Comment