你从ini文件转成文件流FileInputStream然后用 public void load(InputStream inStream) throws IOException 方法就可以加载这个文件,进行相应的操作之后调用 public void store(OutputStream out, String comments) throws IOException 方法进行保存,首先你要有一个FileOutputStream流,你让FileInputStream和FileOutputStream流都指向同一个文件就可以了
类似下面的写法FileOutputStream out = new FileOutputStream(path); PrintStream ps = new PrintStream(out); properties.setProperty(key, value); properties.list(ps); out.close(); ps.close();
不好意思漏写了点,全帖出来public class PropertiesConfiguration { public String path = "" ; private Properties properties; public PropertiesConfiguration(String file){ //String url = this.getClass().getClassLoader().getResource("config.properties").toString(); this.path = file; properties = new Properties(); } public String[] getProperty(String[] key) { String temp[] = new String[key.length]; try { FileInputStream in = new FileInputStream(path); properties.load(in); for(int i= 0 ; i < key.length ; i++){ temp[i] = properties.getProperty(key[i]).trim(); } in.close(); } catch (IOException e) { } return temp ; } public void setProperty(String[] key, String[] value) { try { FileOutputStream out = new FileOutputStream(path); PrintStream ps = new PrintStream(out); for(int i = 0; i<key.length ; i++){ //System.out.println(value[i]); properties.setProperty(key[i], value[i]); } properties.list(ps); out.close(); ps.close(); } catch (IOException e) { System.out.println(e.toString()); } }}
public void load(InputStream inStream)
throws IOException
方法就可以加载这个文件,进行相应的操作之后调用
public void store(OutputStream out,
String comments)
throws IOException
方法进行保存,首先你要有一个FileOutputStream流,你让FileInputStream和FileOutputStream流都指向同一个文件就可以了
PrintStream ps = new PrintStream(out);
properties.setProperty(key, value);
properties.list(ps);
out.close();
ps.close();
public String path = "" ;
private Properties properties;
public PropertiesConfiguration(String file){
//String url = this.getClass().getClassLoader().getResource("config.properties").toString();
this.path = file;
properties = new Properties();
}
public String[] getProperty(String[] key) {
String temp[] = new String[key.length];
try {
FileInputStream in = new FileInputStream(path);
properties.load(in);
for(int i= 0 ; i < key.length ; i++){ temp[i] = properties.getProperty(key[i]).trim();
} in.close();
}
catch (IOException e) {
} return temp ;
} public void setProperty(String[] key, String[] value) { try {
FileOutputStream out = new FileOutputStream(path);
PrintStream ps = new PrintStream(out);
for(int i = 0; i<key.length ; i++){
//System.out.println(value[i]);
properties.setProperty(key[i], value[i]);
} properties.list(ps);
out.close();
ps.close();
}
catch (IOException e) {
System.out.println(e.toString());
} }}