曾经看到有个老师这样写资源关闭,
我的疑问是资源都关闭了( bufferedReader.close();),那这句 bufferedReader = null;还有没有必要写?另外,毕向东老师的视频里好像没有这样写过!
请指教!
public class BufferCopy {
public static void main(String[] args) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader("G:/a.txt"));
bufferedWriter = new BufferedWriter(new FileWriter("H:/a.txt"));
String lines = null;
while ((lines = bufferedReader.readLine()) != null) {
bufferedWriter.write(lines);
bufferedWriter.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedReader != null) {
try {
bufferedReader.close();
// bufferedReader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
|