Properties类(读取主键-值对)是Map子类的Map方法
publicstaticvoidmain (字符串[ ] args ) throws IOException { )。
//读出
属性pro=new properties (;
fileinputstreamfis=new file inputstream (f :demo.properties );
//filereader fr=new filereader (f :demo.properties );
pro.load(fis; //键值的读取由pro.load(fr );
stringstr=pro.getproperty (' name ); //取值
fis.close (; //fr.close (;
//写入
属性pro=new properties (; //创建收藏
pro.setproperty('name ',' lisi ' ); //写键值对
pro.setproperty('hobby ',' eat ' );
pro.setproperty(hobby )、sleep ); //更换值
stringstr=pro.getproperty (' name ); //取值
Set set=pro.stringPropertyNames (; 像keySet那样获取键值的集合
for(stringkey:set )//遍历
system.out.println (key ' . ' pro.getproperty ) key );
}
fileoutputstreamfos=new file output stream (f : (AAA.properties ),true );
//filewriter fw=new filewriter (f :AAA.properties ',true );
pro.store(fos,' person information '; //(文件路径,添加原因) ) ) )。
//pro.store(fw,' save data ';
fos.close (; //5,关闭流
}
//父类的方法:
Properties prop=new Properties (;
PROP.put(czbk001,张gsan ); //添加映射关系
Set keys=prop.keySet (;
for(objectkey:keys ) {
对象值=prop.get (key );
system.out.println(key'='value;
}
Set entrys=prop.entrySet (;
for(map.entryentry:entrys ) {
Object key=entry.getKey (;
Object value=entry.getValue (;
system.out.println(key'='value;
}
printwriter pw=new printwriter (' d.txt ); //打印流对象
POP.list(pw;
pw.close (; //释放资源
Properties类JDBC从文件中读取信息
公共静态连接获取器(
连接连接=null; //不是局部
属性pro=new properties (;
try{
fileinputstreamfis=new file inputstream (src/pro.properties );
pro.load(fis; //相对路径正斜线
class.forname (pro.getproperty (' driver class ' ); 点击try catch
stringURL=pro.getproperty('URL );
string username=pro.getproperty (' username );
string password=pro.getproperty (' password ); //空
conn=驱动程序管理器. getconnection (URL,username,password ); //点添加捕获
}
catch(classnotfoundexceptione ) { e.print堆栈跟踪); }
catch(sqlexceptione ) {e.printStackTrace ); }
catch(ioexceptione ) { e.print堆栈跟踪}; }
返回连接;
}
src/pro.properties文件://无空格密码空字符串(空格) )。
driver class=com.MySQL.JDBC.driver
URL=JDBC : MySQL ://localhost :3306/guanjiapo? use unicode=truezldbm=utf-8 zerodatetimebehavior=converttonull
username=root
password=