如何在不关闭底层System.in的情况下关闭扫描器?

如果我关闭一个扫描器对象并创建一个新对象并尝试读取更多输入, NoSuchElementException收到NoSuchElementException异常。

我的代码工作正常,但如果我不关闭扫描仪,它会给我一个警告。 但是,如果我关闭它来摆脱警告,我也关闭System.in ...我如何避免这种情况?

另外,是否有没有关闭扫描仪的后果?

编辑:这是我的代码:

这是NameAddressExists()方法:

public void NameAddressExists() {
    System.out.println("Enter name");
    Scanner sc = new Scanner(System.in);
    String n = sc.next();
    System.out.println("Enter address");
    String a = sc.next();
    int flag = 0;
    for(int i = 0; i < count; i++) {
        if(array[i].name .equals(n) && array[i].address .equals(a)) {
            System.out.println("True");
            flag = 1;
        }
    }
    if(flag != 1) {
        new Agency(n, a);
    }
    sc.close();
}

这是PanNumberExists()方法:

public boolean PanNumberExists() {
    Scanner s = new Scanner(System.in);
    String n = "";
    System.out.println("Enter the 5 digits");
    try {
        n = s.nextLine();
    }catch(Exception e) {
        System.out.println(e);
    }finally {
        s.close();
    }
    if(n  .equals(this.PAN.subSequence(4,9))) {
        return true;
    }
    else {
        return false;
    }
}

这些方法从以下main()方法中调用:

public static void main(String args[]) {
    Agency obj1 = new Agency("XYZ", "ABCD");
    Agency obj2 = new Agency("XYZ", "ABCDEFG", "+91083226852521", "ab 1234567", "abcd12345ab");
    // Agency obj3 = new Agency("XYZ", "TSRK", "36", "ab 1234567", "abcd12345ab");
    obj1.NameAddressExists();
    System.out.println(obj2.PanNumberExists());
}

如您所见,我首先调用NameAddressExists()方法,在该方法中打开,使用并关闭名为'sc'的Scanner 。 这工作正常,并给我正确的输出。 接下来,我调用PanNumberExists()方法,在该方法中,我打开另一个名为's'的Scanner并尝试使用它来获取用户的一些输入。 这是我收到NoSuchElementException异常的地方。 如果我离开了Scanner “SC”在我NameAddressExists open()方法的话,我没有得到这个错误。


Scanner scan = new Scanner(new FilterInputStream(System.in) {
    @Override
    public void close() throws IOException {
        // do nothing here ! 
    }
});

或者,只需通过实现自定义装饰器忽略close()

public class UnClosableDecorator extends InputStream {

    private final InputStream inputStream;

    public UnClosableDecorator(InputStream inputStream) {
        this.inputStream = inputStream;
    }

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

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

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

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

    @Override
    public int available() throws IOException {
        return 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() {
        return inputStream.markSupported();
    }

    @Override
    public void close() throws IOException {
        //do nothing
    }
}

main()使用时,

public static void main(String[] args) throws Exception {
        System.setIn(new UnClosableDecorator(System.in));
}

您可以使用Decorator模式并创建无法关闭的自定义InputStream ,然后将其传递给Scanner

import java.io.IOException;
import java.io.InputStream;

public class PreventClosingInputStream extends InputStream {

    private InputStream inputStream;

    public PreventClosingInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }

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

    @Override
    public void close() throws IOException {
        // Don't call super.close();
    }

}

然后,在你的代码中:

PreventClosingInputStream in = new PreventClosingInputStream(System.in);
Scanner s = new Scanner(in);
// ...
s.close(); // This will never close System.in as there is underlying PreventClosingInputStream with empty close() method

使用try-with-resources:

try (PreventClosingInputStream in = new PreventClosingInputStream(System.in);
        Scanner s = new Scanner(in);) {
    // ...
    // resources will be automatically closed except of System.in
}
链接地址: http://www.djcxy.com/p/96111.html

上一篇: How to close a scanner without closing the underlying System.in?

下一篇: Java Multiple Scanners