当用property对象的setProperty(key,value)方法时,此时key必须在配置文件中存在的,如果找不到key,就会报空指针异常。
例如配置文件中:
name = Gate
如果想设置name的值为Gate,配置文件中必须先有"name"这个KEY存在。我的问题是能不能一起把key也创建了,就是我的配置文件初始是空的,setProperty()时,如果没有KEY,就先创建KEY,再设置KEY的VALUE
例如配置文件中:
name = Gate
如果想设置name的值为Gate,配置文件中必须先有"name"这个KEY存在。我的问题是能不能一起把key也创建了,就是我的配置文件初始是空的,setProperty()时,如果没有KEY,就先创建KEY,再设置KEY的VALUE
KEY和VALUE都是动态生成,没法开始就把KEY写到配置文件中
=
等号两边都是空
Property.get(Object key)
是根据key取value的值,如果key是NULL,则空指针异常
给一个实例自己看吧
import java.util.*;
import java.io.*;
public class PropertiesDemo{
public static void main(String [] args){
Properties p=new Properties();
p.setProperty("10497","lichao");
p.setProperty("10498","liuyi");
p.setProperty("10697","wangwu");
p.setProperty("10347","wujaxiqi");
saveProperty(p);
Properties p2=new Properties();
loadProperty(p2);
p2.list(System.out);
}
static void saveProperty(Properties p){
FileOutputStream fs=null;
try{
fs=new FileOutputStream("pro.txt");
p.store(fs,"myheader");
}catch(Exception e){
e.printStackTrace();
}finally{
try{
fs.close();
}catch(Exception e){}
}
}
static void loadProperty(Properties p){
FileInputStream fis=null;
try{
fis=new FileInputStream("pro.txt");
p.load(fis);
}catch(Exception e){
e.printStackTrace();
}finally{
try{
fis.close();
}catch(Exception e){}
}
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;public class PropertiesTest {
static File file = new File("test.properties");
public static void main(String[] args) {
try {
createFile();
readFile(file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 写文件
* @throws IOException
*/
public static void createFile() throws IOException{
if(file.exists())
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Map<String,String> map = new HashMap<String,String>();
map.put("1", "2");
map.put("3", "4");
map.put("34", "12");
map.put("5", "6");
FileOutputStream out =new FileOutputStream(file);
for(Map.Entry<String, String> maps : map.entrySet()){
out.write(maps.getKey().getBytes());
out.write("=".getBytes());
out.write(maps.getValue().getBytes());
out.write("\n".getBytes());
}
}
/**
* 读文件
* @param file
* @throws IOException
*/
public static void readFile(File file) throws IOException{
if(!file.exists())
return;
Properties props = new Properties();
InputStream inStream = new FileInputStream(file);
props.load(inStream);
for(Map.Entry<Object, Object> maps : props.entrySet()){
System.out.println(maps.getKey().toString()+":"+maps.getValue().toString());
}
}
}
因为我的配置文件是空的,肯本没有“10497”这个KEY,给他set Value肯定报错
我的意思是要把KEY和VALUE一起保存,而不是先把KEY写到文件中,然后在通过KEY设置它的值看来用配置文件这种方式是做不到了
写的时候:
这种思路可以避免不存在KEY时报异常的问题,但是当我第二次设置map的KEY和VALUE,再进行保存,它会把第一次写进去的内容覆盖掉,就是说不能续写;
读的时候:
你的方法是把配置文件所有的KEY-VALUE迭代出来,我想要的是根据某个KEY取到它对应的VALUE,但是迭代的这种方式,map.getKey(),里面是不能带参数的,就是说不能获得指定KEY的值
读的时候,因为配置文件是KEY=VALUE的形式,我要按照传进来的KEY取得等号后面的VALUE值,和普通文件是不同的。
写的时候:
在原文件后追加文本,这样就不会将原有的key=value覆盖了,如果第二次的map与第一次保存的map有重复的key,你是怎么处理呢??读的时候:
自己写一个getValue("key")方法,迭代所有的key,将value返回.
自己写一个getValue("key")方法,迭代所有的key,将value返回.
=======================================================
读的时候:
用getProperty(String key) 就可以了