流关闭且未重新打开-Java [英] Stream closed and not reopened - Java

查看:196
本文介绍了流关闭且未重新打开-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以轻松完成作业",但是我发现输入流的关闭存在一些问题. 简而言之,我必须使用Java创建一个联系人列表"应用程序,以便以正确的方式使用多态性.因此,我有一个Contact类和一个Private类(contact)子类.在这两个类中,都有一个Modify方法来更改变量的值.

I have an easy 'homework' to do, but I have found a little problem with the closure of the input stream. To put it simply, I have to make a contact 'list' application in Java, just to use the polymorphism in the correct way. So I have a class Contact and a subclass Private (contact). In both class there is a modify method to change the value of the variables.

public void modify() throws IOException {
    System.out.println("Previously name: " + name);
    System.out.println("Insert new name");
    try(InputStreamReader ir = new InputStreamReader(System.in);    
    BufferedReader in = new BufferedReader(ir) ) {  
        name= in.readLine();
        System.out.println("You've changed the name to: "+ name);                       
    System.out.println("Previously surname: " + surname);
    System.out.println("Insert new surname");
        surname= in.readLine();
        System.out.println("You've changed the surname to: "+ surname);                         
    System.out.println("Previously e-mail: " + email);
    System.out.println("Insert new e-mail");
        email = in.readLine();
        System.out.println("You've changed the e-mail to: "+ email);    }                   
}

这是不会造成问题的Contact方法

This is the Contact method that doesn't create problem

@Override 
public void modify() throws IOException {
    super.modifica();
    System.out.println("Numero di cellulare precedente: " + cell);
    System.out.println("Inserire nuovo numero");
    try (InputStreamReader ir = new InputStreamReader(System.in);   
    BufferedReader in = new BufferedReader(ir)) {
        cell = in.readLine();
        System.out.println("Hai cambiato il numero in: "+ cell);                        
    System.out.println("Contatto skype precedente: " + skype);
    System.out.println("Inserire nuovo contatto");
        skype = in.readLine();
        System.out.println("Hai cambiato il contatto in: "+ skype);                         
}   
}

相反,这是Private中方法的替代. 首先,我创建一个Private对象,然后调用Modify方法.我可以毫无问题地插入名称,姓氏和电子邮件,然后该方法将引发IO异常,因为流已关闭. 我不明白为什么会有这种问题.我认为通过尝试使用第一个代码中的资源来关闭流,但是随后通过尝试使用资源的第二个代码中来打开流.我的想法可能是错误的.

Instead, this is the override of the method in Private. In the main, I create a Private object and I call the modify method. I can insert name, surname and e-mail without problem, then the method throws an IO exception because the stream is closed. I can't understand why I have this kind of problem. I think that the stream is closed by try with resources in the first code, but then it is opened in the second code by another try with resources. Probably something in my idea is wrong.

推荐答案

您的问题确实是由于try-with-resource语句关闭了new InputStreamReader(System.in)所致,而该语句在后台也关闭了System.in的基础输入流. (inSystempublic static字段),以致在您的modify方法中System.in已关闭并且无法再读取,这就是为什么会出现此异常的原因.

Your problem is indeed due to the try-with-resource statement that closes new InputStreamReader(System.in) which behind the scene closes also the underlying input stream that is System.in (in is a public static field of System) such that in your modify method System.in is already closed and then cannot be read anymore this is why you get this exception.

这篇关于流关闭且未重新打开-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆