import java.io.*;
import java.util.*;
public class PropertiesDemo {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
//getSystem();
//setAndGet();
//method_1();
loadDemo();
}
public static void loadDemo() throws IOException {//修改配置文件
FileInputStream fis=new FileInputStream("e:\\1.txt");
Properties prop=new Properties();
prop.load(fis);
//sop(prop);//数据是否存到集合了
prop.setProperty("wangwu", 23+"");//这个方法时改变内存而已,Properties有store方法
FileOutputStream fos=new FileOutputStream("e:\\1.txt");
prop.store(fos, "lalal");//comments:注释。爱写不写都可以。不要写中文,中文读不出来
prop.list(System.out);
fis.close();
fos.close();
}
public static void method_1(){//load原理
Properties prop=new Properties();
BufferedReader bufi=null;
try{
bufi=new BufferedReader(new FileReader("e:\\1.txt"));
String line=null;
while((line=bufi.readLine())!=null){
String[] arr=line.split("=");//拿"="切割(等号没了)
//sop(arr[0]+"...."+arr[1]);
prop.setProperty(arr[0], arr[1]);
}
sop(prop);
}catch(IOException e){
throw new RuntimeException("文件读取失败");
}finally{
try{
if(bufi!=null)
bufi.close();
}catch(IOException e){
throw new RuntimeException("文件关闭失败");
}
}
}
//设置获取元素
public static void setAndGet(){
Properties prop=new Properties();
prop.setProperty("zhangsan", "30");//设置键值对:里面的键值都是String类型
prop.setProperty("lisi","39");
//sop(prop);
String value=prop.getProperty("lisi");//Properties自己的方法(当然可以也用Map的,类似:原理是获取键,然后将键传给get(),得到对应的value)
//sop(value);
//现在想拿所有的键值对,Properties是继承hashTable<k,v>,所以只要拿键就能获得值keySet
//Set<Object> keys=prop.keySet();
/*for(Object key:prop.keySet()){
String s=(String)prop.get(key);
sop(key+s);
}此方法是用了父类HashTable的方法,但需要强转,并且Properties有自己的方法当然用自己的方法了
*/
prop.setProperty("lisi", 89+"");//我想修改lisi的值
Set<String> names=prop.stringPropertyNames();//将键值存到Set集合,类似Map将键存到Set集合,只不过这里用了Properties自己的方法
for(String s:names){
sop(s+":"+prop.getProperty(s));
}
}
public static void getSystemProperties(){
Properties pro=System.getProperties();
pro.list(System.out);//list() 以键值对的形式打印在控制台
//sop(pro);
}
public static void sop(Object obj){
System.out.println(obj);
}
}
import java.util.*;
public class PropertiesDemo {
/**
* @param args
*/
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
//getSystem();
//setAndGet();
//method_1();
loadDemo();
}
public static void loadDemo() throws IOException {//修改配置文件
FileInputStream fis=new FileInputStream("e:\\1.txt");
Properties prop=new Properties();
prop.load(fis);
//sop(prop);//数据是否存到集合了
prop.setProperty("wangwu", 23+"");//这个方法时改变内存而已,Properties有store方法
FileOutputStream fos=new FileOutputStream("e:\\1.txt");
prop.store(fos, "lalal");//comments:注释。爱写不写都可以。不要写中文,中文读不出来
prop.list(System.out);
fis.close();
fos.close();
}
public static void method_1(){//load原理
Properties prop=new Properties();
BufferedReader bufi=null;
try{
bufi=new BufferedReader(new FileReader("e:\\1.txt"));
String line=null;
while((line=bufi.readLine())!=null){
String[] arr=line.split("=");//拿"="切割(等号没了)
//sop(arr[0]+"...."+arr[1]);
prop.setProperty(arr[0], arr[1]);
}
sop(prop);
}catch(IOException e){
throw new RuntimeException("文件读取失败");
}finally{
try{
if(bufi!=null)
bufi.close();
}catch(IOException e){
throw new RuntimeException("文件关闭失败");
}
}
}
//设置获取元素
public static void setAndGet(){
Properties prop=new Properties();
prop.setProperty("zhangsan", "30");//设置键值对:里面的键值都是String类型
prop.setProperty("lisi","39");
//sop(prop);
String value=prop.getProperty("lisi");//Properties自己的方法(当然可以也用Map的,类似:原理是获取键,然后将键传给get(),得到对应的value)
//sop(value);
//现在想拿所有的键值对,Properties是继承hashTable<k,v>,所以只要拿键就能获得值keySet
//Set<Object> keys=prop.keySet();
/*for(Object key:prop.keySet()){
String s=(String)prop.get(key);
sop(key+s);
}此方法是用了父类HashTable的方法,但需要强转,并且Properties有自己的方法当然用自己的方法了
*/
prop.setProperty("lisi", 89+"");//我想修改lisi的值
Set<String> names=prop.stringPropertyNames();//将键值存到Set集合,类似Map将键存到Set集合,只不过这里用了Properties自己的方法
for(String s:names){
sop(s+":"+prop.getProperty(s));
}
}
public static void getSystemProperties(){
Properties pro=System.getProperties();
pro.list(System.out);//list() 以键值对的形式打印在控制台
//sop(pro);
}
public static void sop(Object obj){
System.out.println(obj);
}
}