package com.jugiven.service;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/** * @author 作者 zhuzw
* @date 创建时间:2015-12-1 下午2:05:48
* @version 1.0
* @parameter
* @since
* @return
*/
public class TestProper {
public static void main(String[] args) {
String value="";
//第一步是取得一个Properties对象
Properties pro = new Properties();
try {
//获取配置文件的输入流,//在非WEB环境下用这种方式比较方便
InputStream is = TestProper.class.getClassLoader().getResourceAsStream("test.properties");
//在web环境下,用这个
//InputStream is = new FileInputStream("test.properties");
//第三步讲配置文件的输入流load到Properties对象中,这样在后面就可以直接取来用了
pro.load(is);
value = pro.getProperty("fileSend");
System.out.println(value);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
好的,接下来堆properties格式的文件进行写操作
public class TestProper {
public static void main(String[] args) {
Map map = new HashMap();
map.put("place", "E:\\bms\\sen");
TestProper.setProperties(map);
}
public static void setProperties(Map<String,String> data){
Properties props = new Properties();
InputStream is = TestProper.class.getClassLoader().getResourceAsStream("test.properties");
try {
props.load(is);
if(data!=null){
for(Iterator<Entry<String,String>> iterator = data.entrySet().iterator();iterator.hasNext();){
Entry<String,String> entry = iterator.next();
props.setProperty(entry.getKey().toString(), entry.getValue().toString());
OutputStream out = new FileOutputStream("test.properties");
props.store(out, null);
System.out.println(props.toString());
is.close();
out.close();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}