How to read data from a configuration file in Java
- 2020-04-01 02:06:45
- OfStack
1. First create a package (e.g. : config) in the project, then create a configuration file (e.g. : a.perties), add the configuration information as follows:
Such as:
2. Code:
Such as:
name=kaka
age=28
2. Code:
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertyTest {
public static void main(String[] args) {
PropertyTest loadProp = new PropertyTest();
InputStream in = loadProp.getClass().getResourceAsStream("/config/a.properties");
Properties prop = new Properties();
try {
prop.load(in);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(prop.getProperty("name"));
System.out.println(prop.getProperty("age"));
}
}