Fork me on GitHub

Maven项目中读取properties配置文件总结

目录

  • 背景
  • 第一部分 读取properties配置文件方法
  • 参考文献及资料

背景

java开发中,通常将配置信息存储在特定的配置文件中,而不是内嵌在程序代码中。即代码可配置化。properties文件以key=value键值对形式表达,结构比较简单,但是难以表达层次,适合小型项目。本文总结汇总了读取properties配置文件方法。

第一部分 读取properties配置文件方法

为了讲解方便,我们在maven项目的资源目录resources中创建配置文件application.properties。文件内容为:

1
2
username=app
password=password123

1.1 基于ClassLodergetResourceAsStream方法读取配置文件

本方法基于ClassLodergetResourceAsStream方法,通过类加载器来定位资源,返回InputStream后用Properties对象进行加载。

1
2
3
4
5
6
7
8
9
public class configRead {
public static void main(String[] args) throws IOException {
InputStream in = configRead.class
.getClassLoader().getResourceAsStream("application.properties");
Properties properties = new Properties();
properties.load(in);
System.out.println(properties.getProperty("username"));
}
}

1.2 基于getResourceAsStream()方法读取配置文件

利用classgetResourceAsStream方法来定位资源文件,并且直接返回InputStream对象,然后通过Properties进行加载。

1
2
3
4
5
6
7
8
9
10
public class configRead {
public static void main(String[] args) throws IOException {
InputStream In =configRead.class
.getResourceAsStream("application.properties");
Properties properties = new Properties();
properties.load(in);
System.out.println(properties.getProperty("username"));
//app
}
}

1.3 基于ClassLoader类的getSystemResourceAsStream()静态方法读取配置文件

使用ClassLoadergetSystemResourceAsStream()静态方法来定位资源,并且返回InputStream,最后用Properties来加载。

1
2
3
4
5
6
7
8
9
public class configRead {
public static void main(String[] args) throws IOException {
InputStream in = ClassLoader
.getSystemResourceAsStream("application.properties");
Properties properties = new Properties();
properties.load(in);
System.out.println(properties.getProperty("username"));
}
}

1.4 基于FileInputStream读取配置文件

这种方法通过类的路径来定位properties文件资源的路径,然后通过FileInputStream读取流,最后通过java.util.Properties类的load()方法来加载数据。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class configRead {
public static void main(String[] args) throws IOException {
URL url = configRead.class.getClassLoader()
.getResource("application.properties");
if (url != null) {
String fileName = url.getFile();
InputStream in = new BufferedInputStream(
new FileInputStream(fileName));
Properties properties = new Properties();
properties.load(in);
System.out.println(properties.getProperty("username"));
}
}
}

1.5 基于ResourceBundle读取配置文件

利用ResourceBundle来读取properties文件。

1
2
3
4
5
6
7
8
public class configRead {
public static void main(String[] args) throws IOException {
ResourceBundle resourceBundle = ResourceBundle
.getBundle("application.properties", locale1);
System.out.println(resourceBundle.getString("username"));

}
}

1.6 基于PropertyResourceBundle读取配置文件

PropertyResourceBundleResourceBundle的子类,同样我们也可以利用PropertyResourceBundle来加载配置文件的数据,具体的示例如下:

1
2
3
4
5
6
7
8
9
10
public class configRead {
public static void main(String[] args) throws IOException {
URL url = configRead.class.getClassLoader().getResource("application.properties");
if (url != null) {
InputStream in = new BufferedInputStream(new FileInputStream(url.getFile()));
ResourceBundle resourceBundle = new PropertyResourceBundle(in);
System.out.println(resourceBundle.getString("username"));
}
}
}

参考文献及资料

1、Properties files,链接:https://commons.apache.org/proper/commons-configuration/userguide/howto_properties.html

0%