Java properties文件用法

时间:2021-01-26 02:22:52
package com.suyang.properties;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties; public class TestProperties { private final String PATH = "test.properties";
private static Properties props = new Properties(); static {
try {
props.load(new FileInputStream("test.properties"));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} public String getProperty(String name) {
return props.getProperty(name);
} public void setProperty(String name, String value) {
props.setProperty(name, value);
} public void save(){
OutputStream fos = null;
try {
fos = new FileOutputStream(PATH);
props.store(fos, "");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}