我有下面的代碼如何關閉的FileInputStream?如果是的話,我該怎麼做?我的代碼清單中出現錯誤的練習錯誤。要求它終於阻止。在讀取屬性文件
Q
在讀取屬性文件
7
A
回答
8
您必須關閉FileInputStream
,因爲Properties
實例不會。來自Properties.load()
javadoc:
在此方法返回後,指定的流將保持打開狀態。
商店FileInputStream
在一個單獨的變量,宣告了try
之外,並添加finally
塊關閉FileInputStream
,如果它被打開了:
Properties properties = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream("filename.properties");
properties.load(fis);
} catch (FileNotFoundException e) {
system.out.println("FileNotFound");
} catch (IOException e) {
system.out.println("IOEXCeption");
} finally {
if (null != fis)
{
try
{
fis.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
使用try-with-resources自從Java 7:
final Properties properties = new Properties();
try (final FileInputStream fis =
new FileInputStream("filename.properties"))
{
properties.load(fis);
} catch (FileNotFoundException e) {
system.out.println("FileNotFound");
} catch (IOException e) {
system.out.println("IOEXCeption");
}
2
你應該總是請關閉您的流,並在finally
塊中執行此操作是一種很好的做法。原因是finally
塊總是被執行,並且您想要確保流始終是關閉的,即使發生了錯誤。
FileInputStream inStream = null;
try {
inStream = new FileInputStream("filename.properties");
properties.load(inStream);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
} catch (IOException e) {
System.out.println("IOEXCeption");
} finally {
try {
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
如果您使用的是Java 7,這變得更加容易,因爲一個新的try-with
語法進行了介紹。然後你可以這樣寫:
try(FileInputStream inStream = new FileInputStream("filename.properties")){
properties.load(inStream);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
} catch (IOException e) {
System.out.println("IOEXCeption");
}
並且流自動關閉。
1
這裏有一個例子:
public class PropertiesHelper {
public static Properties loadFromFile(String file) throws IOException {
Properties properties = new Properties();
FileInputStream stream = new FileInputStream(file);
try {
properties.load(stream);
} finally {
stream.close();
}
return properties;
}
}
1
您可以使用龍目島@Cleanup簡單地做到這一點。 http://projectlombok.org/features/Cleanup.html
Properties properties = new Properties();
try {
@Cleanup FileInputStream myFis = new FileInputStream("filename.properties");
properties.load(myFis);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
}catch (IOException e) {
System.out.println("IOEXCeption");
}
或者,只有當您使用的是Java 7,有「嘗試用資源」的新功能。 http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
Properties properties = new Properties();
try (FileInputStream myFis = new FileInputStream("filename.properties")) {
properties.load(myFis);
} catch (FileNotFoundException e) {
System.out.println("FileNotFound");
}catch (IOException e) {
System.out.println("IOEXCeption");
}
相關問題
- 1. 從屬性文件讀取
- 2. JSP屬性文件讀取
- 3. 讀取屬性文件
- 4. 讀取屬性文件在Spring 3.2
- 5. 在Spring Boot中讀取屬性文件
- 6. 在Spring中讀取屬性文件
- 7. 在java中讀取屬性文件
- 8. Maven,使用屬性文件讀取另一個屬性文件
- 9. 如何在j2me中讀取文件的屬性/屬性
- 10. 試圖讀取xml文件[屬性]
- 11. 如何讀取屬性文件?
- 12. 克朗無法讀取屬性文件
- 13. 從屬性文件讀取Spring 3
- 14. 無法讀取屬性文件
- 15. 從屬性文件中讀取值
- 16. UNIX外殼與屬性文件讀取
- 17. 讀取屬性文件到不工作
- 18. angularjs從屬性文件中讀取
- 19. Spring Roo&讀取屬性文件
- 20. RestFul web服務讀取屬性文件
- 21. 讀取屬性文件使用JavaScript
- 22. 用java讀取文件屬性
- 23. 駱駝讀取屬性文件
- 24. 如何從HDF5文件讀取屬性?
- 25. 無法讀取AppletViewer屬性文件 - Applet
- 26. 讀取Pyspark中的屬性文件
- 27. 通過NFS讀取UNIX文件屬性
- 28. 讀取服務器文件的屬性
- 29. 如何從屬性文件讀取值?
- 30. 從.properties文件讀取屬性與JavaFX
「Properties properties = new」 - wrong。 – duffymo 2012-07-11 10:16:59