java中获取properties_java中读取Properties文件----五种方式

public classTestProperties {public String path1="com/pengzhen/test/jdbc.properties";public String path2="/com/pengzhen/test/jdbc.properties";public String path3="src/com/pengzhen/test/jdbc.properties";public String path4="com/pengzhen/test/jdbc";public String path5="src/com/pengzhen/test/jdbc.properties";/*第一种采用的是类加载器*/

public voidWayOne(){

ClassLoader loader=this.getClass().getClassLoader();

InputStream is=loader.getResourceAsStream(path1);

Properties properties=newProperties();try{

properties.load(is);

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

String url=properties.getProperty("jdbc.url");

String username=properties.getProperty("jdbc.username");

System.out.println(url+"||"+username);

}/*第二种采用的是运行类本身的方法*/

public voidWayTwo(){

InputStream is=this.getClass().getResourceAsStream(path2);

Properties properties=newProperties();try{

properties.load(is);

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

String url=properties.getProperty("jdbc.url");

String username=properties.getProperty("jdbc.username");

System.out.println(url+"||"+username);

}/*第三种采用的是文件流的形式,并用Properties.load方法加载该流*/

public voidWayThree(){

InputStream is= null;try{

is= new BufferedInputStream(new FileInputStream(new File("src/com/pengzhen/test/jdbc.properties")));

}catch(FileNotFoundException e1) {//TODO Auto-generated catch block

e1.printStackTrace();

}

Properties properties=newProperties();try{

properties.load(is);

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

String url=properties.getProperty("jdbc.url");

String username=properties.getProperty("jdbc.username");

System.out.println(url+"||"+username);

}/*第四种采用的是ResourceBundle的getBundle方法*/

public voidWayFour(){

ResourceBundle rBundle=ResourceBundle.getBundle(path4);

String url=rBundle.getString("jdbc.url");

String username=rBundle.getString("jdbc.username");

System.out.println(url+"||"+username);

}/*第五种采用的是PropertyResourceBundle类的构造方法*/

public voidWayFive(){

InputStream is= null;try{

is= new BufferedInputStream(newFileInputStream(path5));

}catch(FileNotFoundException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

PropertyResourceBundle propertyResourceBundle= null;try{

propertyResourceBundle= newPropertyResourceBundle(is);

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

String url=propertyResourceBundle.getString("jdbc.url");

String username=propertyResourceBundle.getString("jdbc.username");

System.out.println(url+"||"+username);

}public static voidmain(String[] args) {

TestProperties properties=newTestProperties();

properties.WayOne();

properties.WayTwo();

properties.WayThree();

properties.WayFour();

properties.WayFive();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值