java读取外部配置文件
时间: 2023-07-18 22:18:14 浏览: 75
在Java中读取外部配置文件可以使用Properties类来实现。
下面是一个简单的示例代码:
```
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
public class ConfigReader {
private Properties prop;
public ConfigReader(String fileName) {
prop = new Properties();
try {
FileInputStream fis = new FileInputStream(fileName);
prop.load(fis);
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getProperty(String key) {
return prop.getProperty(key);
}
}
```
使用方法:
```
ConfigReader reader = new ConfigReader("config.properties");
String value = reader.getProperty("key");
```
其中,config.properties是外部配置文件的文件名,key是配置项的名称。通过getProperty方法可以获取对应配置项的值。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)