构造函数是否在初始化后new FileInputStream(someFile);
调用方法?close()
初始化后我需要调用close()
这个对象吗?
问问题
127 次
3 回答
1
不,它只是打开流;由您决定何时关闭它。
于 2012-04-28T12:31:34.877 回答
1
不,close()
构造函数不会调用该方法,因此您应该在使用特定 FileInputStream 实例完成后调用它。
于 2012-04-28T12:32:23.097 回答
1
如果您忘记执行此操作,通常会在程序终止或文件流对象被垃圾回收时自动关闭文件,但最好在完成文件后立即关闭文件。
File file = new File("DevFile.txt"); // This will create file object with meta info
int ch;
StringBuffer strContent = new StringBuffer("");
FileInputStream fin = null;
try {
fin = new FileInputStream(file); // It'll open a stream and type is input
while ((ch = fin.read()) != -1)// and you can read data stream unless it is closed
strContent.append((char) ch);
fin.close(); // you should close stream to provide safety of your file
} catch (FileNotFoundException e) {
} catch (IOException ioe) {
}
于 2012-04-28T12:33:31.097 回答