我们是否需要在finally块中将进程变量设置为null?

OVB*_*OVB 5 java process

如果我们有以下代码:

Process p = null;
BufferedReader br = null;

try{
    p = Runtime.getRuntime().exec("ps -ef");
    br = new BufferedReader(new InputStreamReader(p.getInputStream()));
    //Do something with br
} catch (Exception e) {
    //Handle catch block
} finally {
    //Do we need to set p = null;
}
Run Code Online (Sandbox Code Playgroud)

finally块中是否需要p = null,或者默认情况下关闭所有关联的流?

Ósc*_*pez 10

有没有必要设置过程null,但它会是一个好主意,明确关闭BufferedReaderfinally块.或者甚至更好,如果使用Java 7或更高版本,请考虑使用try资源自动关闭流.