Read the Properties file using Java

1. Create the file.properties file in the resources folder

1
2
3
name=Jack
age=18
sex=male

2. Use code to read the file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class InputProperties {

public static void main(String[] args) throws IOException {
ClassLoader classLoader = InputProperties.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("file.properties");

Properties properties = new Properties();
properties.load(inputStream);
System.out.println(properties.getProperty("name"));
System.out.println(properties.getProperty("age"));
System.out.println(properties.getProperty("sex"));
}

}

Read the Properties file using Java
https://www.hardyhu.cn/2022/08/03/Read-the-Properties-file-using-Java/
Author
John Doe
Posted on
August 3, 2022
Licensed under