File f
=
new
File(
"
c:\\my.properties
"
);
Properties prop = new Properties();
if (f.exists()) {
try {
InputStream in = new FileInputStream(f);
prop.load(in);
in.close();
System.out.println(prop.get( " requestTime " )); // 读properties文件
OutputStream out = new FileOutputStream(f);
prop.setProperty( " derek " , " dada " ); // 设置属性
prop.store(out, " THE LOCKED TIME SET " ); // 写properties文件
} catch (FileNotFoundException ex) {ex.printStackTrace();
} catch (IOException ex) {ex.printStackTrace();}
} else System.out.println( " 文件不存在 " );
Properties prop = new Properties();
if (f.exists()) {
try {
InputStream in = new FileInputStream(f);
prop.load(in);
in.close();
System.out.println(prop.get( " requestTime " )); // 读properties文件
OutputStream out = new FileOutputStream(f);
prop.setProperty( " derek " , " dada " ); // 设置属性
prop.store(out, " THE LOCKED TIME SET " ); // 写properties文件
} catch (FileNotFoundException ex) {ex.printStackTrace();
} catch (IOException ex) {ex.printStackTrace();}
} else System.out.println( " 文件不存在 " );
本文介绍了一个使用Java来读取和写入properties文件的具体示例。通过创建File和Properties对象,演示了如何从指定路径加载配置文件,并读取其中的属性值。此外,还展示了如何修改并保存新的属性值到文件中。
2035

被折叠的 条评论
为什么被折叠?



