java从config配置文件中获取参数
时间: 2024-03-26 19:39:49 浏览: 22
在 Java 中,可以使用 Properties 类来读取配置文件中的参数。以下是一个示例代码:
```java
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ConfigReader {
public static void main(String[] args) {
Properties properties = new Properties();
try {
FileInputStream fileInputStream = new FileInputStream("config.properties");
properties.load(fileInputStream);
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
String param1 = properties.getProperty("param1");
String param2 = properties.getProperty("param2");
System.out.println("param1: " + param1);
System.out.println("param2: " + param2);
}
}
```
在这个例子中,我们首先创建了一个 Properties 对象,并且使用 FileInputStream 从文件中读取配置参数。然后,我们可以使用 getProperty 方法来获取指定的参数值。最后,我们可以将参数值打印出来。请注意,这里的 config.properties 文件应该放在 Java 项目的根目录下。