流已关闭且未重新打开 - Java

Stream closed and not reopened - Java

我有一个简单的 'homework' 可以做,但是我发现输入流的关闭有点问题。 简单来说,我必须在 Java 中创建一个联系 'list' 应用程序,以正确的方式使用多态性。所以我有一个 class 联系人和一个 subclass 私人(联系人)。在 class 中都有一个修改方法来更改变量的值。

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);    }                   
}

这是不会产生问题的联系方法

@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 对象并调用了修改方法。我可以毫无问题地插入名字、姓氏和电子邮件,然后该方法会抛出 IO 异常,因为流已关闭。 我不明白为什么我有这种问题。我认为流是在第一个代码中尝试使用资源关闭的,但随后在第二个代码中通过另一个尝试使用资源打开了它。可能是我的想法有问题。

您的问题确实是由于关闭 new InputStreamReader(System.in) 的 try-with-resource 语句造成的,该语句在幕后也关闭了底层输入流 System.inin 是一个public static System) 的字段,这样在您的 modify 方法中 System.in 已经关闭,然后无法再读取,这就是您收到此异常的原因。

如果用 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);
        }
    }
}

另请参阅:Closing BufferedReader and System.in