Java讀取Properties文件的六種方法

使用J2SE API讀取Properties文件的六種方法 1。使用java.util.Properties類的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream(name)); Properties p = new Properties(); p.load(in); 2。使用java.util.ResourceBundle類的getBundle()方法 示例: ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault()); 3。使用java.util.PropertyResourceBundle類的構造函數 示例: InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in); 4。使用class變量的getResourceAsStream()方法 示例: InputStream in = JProperties.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in); 5。使用class.getClassLoader()所獲得的java.lang.ClassLoader的getResourceAsStream()方法 示例: InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in); 6。使用java.lang.ClassLoader類的getSystemResourceAsStream()靜態方法 示例: InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p = new Properties(); p.load(in); 補充 Servlet中可使用javax.servlet.ServletContext的getResourceAsStream()方法 示例:InputStream in = context.getResourceAsStream(path); Properties p = new Properties(); p.load(in);
相關文章
相關標籤/搜索