- 通过getResourceAsStream()方法来获取配置文件。
public class ReadProperties {
public static void main(String[] args) {
// readPropFileByGetResourceAsStream();
readPropFileByInPutStream();
}
static void readPropFileByGetResourceAsStream(){
//注:静态代码块中获取当前类不能用 this关键字,同时也不能使用super关键字,试试你就知道他会报什么错啦
//调用对象的getClass()方法是获得对象当前的类类型,这部分数据存在方法区中,
//而后在类类型上调用getClassLoader()方法是得到当前类型的类加载器,
//我们知道在Java中所有的类都是通过加载器加载到虚拟机中的,
//而且类加载器之间存在父子关系,就是子知道父,父不知道子,
//这样不同的子加载的类型之间是无法访问的(虽然它们都被放在方法区中),
//所以在这里通过当前类的加载器来加载资源也就是保证是和类类型同一个加载器加载的。
/*
* 读取src下的config/properties下的配置文件
*/
InputStream in1 = ReadProperties.class.getClassLoader().getResourceAsStream("config/properties/test1.properties");
/*
* 读取和ReadProperties位于同一个包中的配置文件
*/
InputStream in2 = ReadProperties.class.getResourceAsStream("test2.properties");
/*
* 读取src根目录下文件的配置文件
* jdbc.properties位于src目录
*/
InputStream in3 = ReadProperties.class.getClassLoader().getResourceAsStream("jdbc.properties");
/*
* 读取位于另一个source文件夹里面的配置文件
* config是一个source文件夹,config.properties位于config source文件夹中
*/
InputStream in4 = ReadProperties.class.getClassLoader().getResourceAsStream("config.properties");
Properties p = new Properties();
System.out.println("----使用getResourceAsStream方法读取properties文件----");
try {
System.out.println("----------------------------------------------");
p.load(in1);
System.out.println("test1.properties:name=" + p.getProperty("name") + ",age=" + p.getProperty("age"));
System.out.println("----------------------------------------------");
p.load(in2);
System.out.println("test2.properties:name=" + p.getProperty("name") + ",age=" + p.getProperty("age"));
System.out.println("----------------------------------------------");
p.load(in3);
System.out.println("jdbc.properties:");
System.out.println(String.format("jdbc.driver=%s",p.getProperty("jdbc.driver")));// 这里的%s是java String占位符
System.out.println(String.format("jdbc.url=%s",p.getProperty("jdbc.url")));
System.out.println(String.format("jdbc.usename=%s",p.getProperty("jdbc.usename")));
System.out.println(String.format("jdbc.password=%s",p.getProperty("jdbc.password")));
System.out.println("----------------------------------------------");
p.load(in4);
System.out.println("config.properties:");
System.out.println(MessageFormat.format("dbuser={0}",p.getProperty("dbuser")));// {0}是一个java的字符串占位符
System.out.println(MessageFormat.format("dbpassword={0}",p.getProperty("dbpassword")));
System.out.println(MessageFormat.format("database={0}",p.getProperty("database")));
System.out.println("----------------------------------------------");
} catch (IOException e) {
e.printStackTrace();
}finally{
if(in1 != null){
try {
in1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in2 != null){
try {
in2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in3 != null){
try {
in3.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in4 != null){
try {
in4.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
- 通过inputStream来读取配置文件
public class ReadProperties {
public static void main(String[] args) {
// readPropFileByGetResourceAsStream();
readPropFileByInPutStream();
}
static void readPropFileByInPutStream(){
InputStream in1 = null;
InputStream in2 = null;
InputStream in3 = null;
InputStream in4 = null;
System.out.println("----使用InputStream流读取properties文件----");
try {
/**
* 读取src根目录下文件的配置文件
* jdbc.properties位于src目录
*/
in1 = new BufferedInputStream(new FileInputStream("src/jdbc.properties"));
/**
* 读取src下面config.properties包内的配置文件
* test1.properties位于config.properties包内
*/
in2 = new BufferedInputStream(new FileInputStream("src/config/properties/test1.properties"));
/**
* 读取和PropertiesFileReadTest类位于同一个包里面的配置文件
* test2.properties和PropertiesFileReadTest类在同一个包里面
*/
in3 = new BufferedInputStream(new FileInputStream("src/propertiesFile/read/test/test2.properties"));
/**
* 读取位于另一个source文件夹里面的配置文件
* config是一个source文件夹,config.properties位于config source文件夹中
*/
in4 = new FileInputStream("config/config.properties");
Properties p = new Properties();
System.out.println("----------------------------------------------");
p.load(in1);
System.out.println("jdbc.properties:");
System.out.println(String.format("jdbc.driver=%s",p.getProperty("jdbc.driver")));// 这里的%s是java String占位符
System.out.println(String.format("jdbc.url=%s",p.getProperty("jdbc.url")));
System.out.println(String.format("jdbc.usename=%s",p.getProperty("jdbc.usename")));
System.out.println(String.format("jdbc.password=%s",p.getProperty("jdbc.password")));
System.out.println("----------------------------------------------");
p.load(in2);
System.out.println("test1.properties:name=" + p.getProperty("name") + ",age=" + p.getProperty("age"));
System.out.println("----------------------------------------------");
p.load(in3);
System.out.println("test2.properties:name=" + p.getProperty("name") + ",age=" + p.getProperty("age"));
p.load(in4);
System.out.println("config.properties:");
System.out.println(MessageFormat.format("dbuser={0}",p.getProperty("dbuser")));// {0}是一个java的字符串占位符
System.out.println(MessageFormat.format("dbpassword={0}",p.getProperty("dbpassword")));
System.out.println(MessageFormat.format("database={0}",p.getProperty("database")));
System.out.println("----------------------------------------------");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(in1 != null){
try {
in1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in2 != null){
try {
in2.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in3 != null){
try {
in3.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(in4 != null){
try {
in4.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}