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

时间:2016-08-06 18:33:37

标签: java stream override try-with-resources

我有一个简单的'家庭作业',但我发现输入流的关闭有点问题。 简单地说,我必须用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方法。我可以毫无问题地插入名称,姓氏和电子邮件,然后该方法抛出IO异常,因为流已关闭。 我无法理解为什么会遇到这种问题。我认为通过尝试使用第一个代码中的资源来关闭流,但是然后通过另一个尝试使用资源在第二个代码中打开它。可能我的想法中的某些内容是错误的。

2 个答案:

答案 0 :(得分:0)

你的问题确实是由于关闭new InputStreamReader(System.in)的try-with-resource语句,在场景后面关闭了基础输入流System.inin是{{} 1}} public static}的字段,使得System方法modify已经关闭,然后无法再读取,这就是您获得此异常的原因。

答案 1 :(得分:0)

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

我还建议使用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