java properties读取缓存_Java读取Properties文件

深碍√TFBOYSˉ_ 2022-11-03 11:24 468阅读 0赞

有一个properties文件box.properties,内容如下:

Color=Red

Name=Box

Length=18

Width=7

Heigth=8

获取其中的属性值,可用如下代码:

InputStream in = null;

Properties p = new Properties();

try {

in = new BufferedInputStream(new FileInputStream(“box.properties”));

p.load(in);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Enumeration keys = p.keys();

while (keys.hasMoreElements()) {

String key = (String) keys.nextElement();

System.out.println(key + “:” + p.getProperty(key));

}

或者:

InputStream in;

ResourceBundle rb = null;

try {

in = new BufferedInputStream(new FileInputStream(“box.properties”));

rb = new PropertyResourceBundle(in);

} catch (FileNotFoundException e1) {

// TODO Auto-generated catch block

e1.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

if (rb != null) {

Enumeration keys = rb.getKeys();

while (keys.hasMoreElements()) {

String key = (String) keys.nextElement();

System.out.println(key + “:” + rb.getString(key));

}

}

不过输出顺序与原始文件不同。

发表评论

表情:
评论列表 (有 0 条评论,468人围观)

还没有评论,来说两句吧...

相关阅读