2012-07-11 228 views
7

我有下面的代碼如何關閉的FileInputStream?如果是的話,我該怎麼做?我的代碼清單中出現錯誤的練習錯誤。要求它終於阻止。在讀取屬性文件

回答

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; 
    } 
} 
+0

「Properties properties = new」 - wrong。 – duffymo 2012-07-11 10:16:59

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"); 
}