2012-07-11 32 views
7

Tôi đã mã sau:làm thế nào để đóng FileInputStream trong khi đọc các tập tin bất động sản

// Read properties file. 
    Properties properties = new Properties(); 
    try { 
    properties.load(new FileInputStream("filename.properties")); 
    } catch (FileNotFoundException e) { 
    system.out.println("FileNotFound"); 
    }catch (IOException e) { 
    system.out.println("IOEXCeption"); 
    } 

Có cần thiết để đóng FileInputStream? Nếu có, làm thế nào để tôi làm điều đó? Tôi đang gặp phải lỗi thực hành không tốt trong danh sách kiểm tra mã của tôi. Yêu cầu nó đặt cuối cùng khối.

Trả lời

8

Bạn phải đóng FileInputStream, vì trường hợp Properties sẽ không. Từ số Properties.load() javadoc:

Luồng được chỉ định vẫn mở sau khi phương thức này trả về.

Store FileInputStream trong một biến riêng biệt, tuyên bố bên ngoài của try và thêm một khối finally rằng đóng FileInputStream nếu nó được mở cửa:

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

Sử dụng try-with-resources kể từ 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

Bạn nên luôn luôn s đóng các luồng của bạn và thực hiện nó trong khối finally là một phương pháp hay. Lý do cho điều này là khối finally luôn được thực thi và bạn muốn đảm bảo rằng luồng luôn đóng, ngay cả khi xảy ra sự cố.

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

Nếu bạn đang sử dụng Java 7, điều này trở nên dễ dàng hơn nhiều, vì cú pháp mới try-with đã được giới thiệu. Sau đó, bạn có thể viết như sau:

try(FileInputStream inStream = new FileInputStream("filename.properties")){ 
     properties.load(inStream); 
    } catch (FileNotFoundException e) { 
     System.out.println("FileNotFound"); 
    } catch (IOException e) { 
     System.out.println("IOEXCeption"); 
} 

và luồng sẽ tự động bị đóng.

1

đây là một ví dụ:

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

"Thuộc tính thuộc tính = mới" - sai. – duffymo

1

Bạn có thể sử dụng Lombok @Cleanup để làm điều đó đơn giản. 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"); 
} 

Hoặc, chỉ khi bạn đang sử dụng Java 7, đó là "thử với tài nguyên" tính năng mới. 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"); 
} 
Các vấn đề liên quan