流关闭而不是重新打开 – Java

我有一个简单的’功课’,但我发现输入流的关闭有点问题。 简单来说,我必须用Java创建一个联系人’列表’应用程序,只是以正确的方式使用多态。 所以我有一个类Contact和一个子类Private(联系人)。 在这两个类中都有一个修改方法来更改变量的值。

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方法

 @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中方法的覆盖。 在main中,我创建了一个Private对象,我调用了modify方法。 我可以毫无问题地插入名称,姓氏和电子邮件,然后该方法抛出IOexception,因为流已关闭。 我不明白为什么会遇到这种问题。 我认为通过尝试使用第一个代码中的资源来关闭流,但是然后通过另一个尝试使用资源在第二个代码中打开它。 可能我的想法中的某些内容是错误的。

您的问题确实是由于try-with-resource语句关闭了new InputStreamReader(System.in) ,它在场景后面也关闭了System.in的底层输入流( in Systempublic static字段中),这样你的modify方法System.in已经关闭,然后再也无法读取,这就是你得到这个exception的原因。

如果使用CloseShieldInputStream包装System.in ,仍然可以使用try-with-resources

我还建议使用Scanner而不是InputStreamReaderBufferedReader ,因为它很简单:

 import java.util.Scanner; import org.apache.commons.io.input.CloseShieldInputStream; public class Contact { protected String name; protected String surname; protected String email; public void modify() throws IOException { System.out.println("Previously name: " + name); System.out.println("Insert new name"); try (Scanner sc = new Scanner(new CloseShieldInputStream(System.in))) { name = sc.nextLine(); System.out.println("You've changed the name to: " + name); System.out.println("Previously surname: " + surname); System.out.println("Insert new surname"); surname = sc.nextLine(); 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 = sc.nextLine(); System.out.println("You've changed the e-mail to: " + email); } } } public class Private extends Contact { private String cell; private String skype; @Override public void modify() throws IOException { super.modify(); System.out.println("Numero di cellulare precedente: " + cell); System.out.println("Inserire nuovo numero"); try (Scanner sc = new Scanner(new CloseShieldInputStream(System.in))) { cell = sc.nextLine(); System.out.println("Hai cambiato il numero in: " + cell); System.out.println("Contatto skype precedente: " + skype); System.out.println("Inserire nuovo contatto"); skype = sc.nextLine(); System.out.println("Hai cambiato il contatto in: " + skype); } } } 

另请参阅: 关闭BufferedReader和System.in