2012-04-28 11 views

回答

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) { 
} 
1

不,它只是打开流;您需要决定何时关闭它。

1

不,构造函数不会调用close()方法,所以当您完成使用特定的FileInputStream实例时应该调用它。