读取properties的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15061629/article/details/51581963
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);
}
}


猜你喜欢

转载自blog.csdn.net/qq_15061629/article/details/51581963
今日推荐