读取properties的方法

package com.lks.readpro;


import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Properties;
import java.util.ResourceBundle;


/**
 * 
 * @author lks
 * @time 2016年6月3日上午10:00:39
 */
public class Readerproperties1 {
/**
* 
* (1).想要通过读取properties文件的key和value 1、通过绝对路径:InputStream is=new
* FileInputStream(filePath); 2、通过Class.getResourceAsStream(path);
* 3、通过ClassLoader.getResourceAsStream(path); (2).加载文件通过properties对象中的load()
* (3).关闭流对象
*/
public void read(Properties properties) {
InputStream is = null;
try {
// is = new FileInputStream("src/1.properties");
is = this.getClass().getResourceAsStream("/src/1.properties");
properties.load(is);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
String username = properties.getProperty("username");
String password = properties.getProperty("password");
System.out.println(username + " " + password);
}


/**
* 通过resourceBundle记载文件
*/
public void read1() {
// 加载properties的名称1.properties,只能放在src文件目录下(实验)
ResourceBundle bundle = ResourceBundle.getBundle("1");
Enumeration<String> enumeration = bundle.getKeys();
while (enumeration.hasMoreElements()) {
String e = (String) enumeration.nextElement();
// 输出key值
System.out.println(e);
// 获取文件中的value
String value = bundle.getString(e);
System.out.println(value);


}
}


/**
* 加载的路径问题的解决 1. 类下获取文件,com.lks.readpro-
* readerproperties.class下获取路径通过this.getClass.getResourceAsStream()获取到了文件的目录
* 2.如果是在本类的子类(son)下的话,我们可以通过this.getClass().getResourceAsStream(
* "son/2.properties")
* 
* 注意:在下列首个'/'是表示根目录下,没有'/',表示本目录下
* 3.如果是在同级目录下的话,this.getClass().getResourceAsStream
* ("/com/lks/test/2.properties");
*/
public void read2(Properties properties) {
InputStream is = null;
// 类加载
is = this.getClass().getResourceAsStream("son/2.properties");
try {
properties.load(is);
String username = properties.getProperty("username");
String pass = properties.getProperty("password");
System.out.println(username + " " + pass);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/**
* 通过classloader加载文件
* 
* @param properties
*/
public void read3(Properties properties) {
InputStream is = null;
is = this.getClass().getClassLoader()
.getResourceAsStream("1.properties");
try {
properties.load(is);
String username = properties.getProperty("username");
String pass = properties.getProperty("password");
System.out.println(username + " " + pass);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}


/**
* 
* getResourceAsStream读取的文件路径只局限与工程的源文件夹中,包括在工程src根目录下,以及类包里面任何位置,
* 但是如果配置文件路径是在除了源文件夹之外的其他文件夹中时,该方法是用不了的。
*/
public static void main(String[] args) {


/*
* Properties properties = new Properties(); new
* Readerproperties1().read(properties);
*/
/* new Readerproperties1().read1(); */


Properties p = new Properties();
new Readerproperties1().read3(p);
}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值