以下程序哪里有问题?
try {
FileOutputStream out = null;
BufferedWriter writer = null;
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if ( writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
第二个try块中的writer未定义,为什么呢?因为按照自己的想法是,writer = new BufferedWriter(new OutputStreamWriter(out));这一句已经定义了,但是,定义在try块中了,属于局部变量,在finally中无法使用,所以此处writer是未定义的。