Java学习之读取Yaml文件
1.yaml文件示例:
england:
initialUrl: https://zq.titan007.com/cn/League/36.html
finalUrl:
filePath:
fileName: ""
spain:
initialUrl:
finalUrl:
filePath:
fileName: ""
australia:
initialUrl:
finalUrl:
filePath:
fileName: ""
2.配置pom.xml
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.27</version>
</dependency>
3.读取yaml文件示例代码:
public class BaseInfo {
public static HashMap<String,Object> getBaseInfo(String league) {
try {
//解析yml文件的关键类
FileInputStream inputStream = new FileInputStream(new File("jobParam.yaml"));
Yaml yml = new Yaml();
Map<String,Object> obj = (Map)yml.load(inputStream);
HashMap<String,Object> baseInfo =(HashMap)obj.get(league);
return baseInfo;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}