为什么我们需要 FileInputStream in = null; try块之前的行?
Why do we need FileInputStream in = null; line before try block?
此代码来自 oracle i/o 教程:
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
为什么这些行
FileInputStream in = null;
FileOutputStream out = null;
不包括以这种方式尝试阻止(没有= null
)?
FileInputStream in = new FileInputStream("xanadu.txt");
FileOutputStream out = new FileOutputStream("outagain.txt");
您需要在 try {...}
之外声明 in
和 out
,因为您需要在 finally {...}
块中关闭这些资源。
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
....
} catch(Exception e) {
} finally {
try {
if(in != null) {
in.close();
}
if(out != null) {
out.close();
}
catch(Exception e) {...}
}
如果您在 try {...}
范围内声明它们,编译器会抱怨它们无法解析。
如果你这样做:
try {
FileInputStream in = new FileInputStream("xanadu.txt");
FileOutputStream out = new FileOutputStream("outagain.txt");
int c;
....
} catch(Exception e) {
} finally {
try {
if(in != null) { //in could not be resolved error by compiler
in.close();
}
if(out != null) { //out could not be resolved...
out.close();
}
catch(Exception e) {...}
}
因为 Inputstream 是一个沉重的资源。
您已经打开了一个 FileInputStream,现在在使用它时发生了一些异常。
然后该流将保持打开状态,浪费资源。
所以你在 try 块之前用 null 初始化它,这样你就可以在 finally 块中关闭它,即清理资源的正确方法。
FileInputStream in=null
try{
}catch(IOException ioexcep){
}
finally {
in.close;
}
如果你在 try 块中声明和初始化流,在 finally 语句中如果你想尝试关闭流,编译器不知道 in[=15= 的值是什么] 和 out 以关闭流。
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
} catch(Exception e) {
----
} finally {
if(in!=null) {
in.close();
}
if(out!= null) {
out.close();
}
}
如果您使用的是 Java 7 或更高版本,您可以使用 try-with-resources,它将为您处理关闭。
try(
FileInputStream in = new FileInputStream("xanadu.txt");
FileOutputStream out = new FileOutputStream("outagain.txt");
) {
.... do stuff
} catch (Exception e) {
... deal with the exception
};
这是因为 FileInputStream 实现了 java.lang.AutoCloseable,所以它会在 try 块完成或抛出异常时关闭()。
此代码来自 oracle i/o 教程:
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
为什么这些行
FileInputStream in = null;
FileOutputStream out = null;
不包括以这种方式尝试阻止(没有= null
)?
FileInputStream in = new FileInputStream("xanadu.txt");
FileOutputStream out = new FileOutputStream("outagain.txt");
您需要在 try {...}
之外声明 in
和 out
,因为您需要在 finally {...}
块中关闭这些资源。
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
....
} catch(Exception e) {
} finally {
try {
if(in != null) {
in.close();
}
if(out != null) {
out.close();
}
catch(Exception e) {...}
}
如果您在 try {...}
范围内声明它们,编译器会抱怨它们无法解析。
如果你这样做:
try {
FileInputStream in = new FileInputStream("xanadu.txt");
FileOutputStream out = new FileOutputStream("outagain.txt");
int c;
....
} catch(Exception e) {
} finally {
try {
if(in != null) { //in could not be resolved error by compiler
in.close();
}
if(out != null) { //out could not be resolved...
out.close();
}
catch(Exception e) {...}
}
因为 Inputstream 是一个沉重的资源。
您已经打开了一个 FileInputStream,现在在使用它时发生了一些异常。
然后该流将保持打开状态,浪费资源。
所以你在 try 块之前用 null 初始化它,这样你就可以在 finally 块中关闭它,即清理资源的正确方法。
FileInputStream in=null
try{
}catch(IOException ioexcep){
}
finally {
in.close;
}
如果你在 try 块中声明和初始化流,在 finally 语句中如果你想尝试关闭流,编译器不知道 in[=15= 的值是什么] 和 out 以关闭流。
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
} catch(Exception e) {
----
} finally {
if(in!=null) {
in.close();
}
if(out!= null) {
out.close();
}
}
如果您使用的是 Java 7 或更高版本,您可以使用 try-with-resources,它将为您处理关闭。
try(
FileInputStream in = new FileInputStream("xanadu.txt");
FileOutputStream out = new FileOutputStream("outagain.txt");
) {
.... do stuff
} catch (Exception e) {
... deal with the exception
};
这是因为 FileInputStream 实现了 java.lang.AutoCloseable,所以它会在 try 块完成或抛出异常时关闭()。