版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第javayaml转properties工具类方式目录yaml转properties工具类properties与yml之间的比较发现了几个要注意的地方
yaml转properties工具类
yaml文件转properties文件
yaml字符串转properties字符串
yaml转Map
packagecom.demo.utils;
importlombok.Data;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileWriter;
importjava.io.IOException;
importjava.nio.charset.StandardCharsets;
importjava.util.*;
importjava.util.stream.Stream;
*Yaml配置文件转Properties配置文件工具类
publicclassYmlUtils{
privatestaticfinalStringlineSeparator="\n";
*将yml字符串化为properties字符串
*@paramyml
*@return
publicstaticStringyamlStr2PropStr(Stringyml){
ListYmlNodenodeList=getNodeList(yml);
//去掉多余数据,并打印
Stringstr=printNodeList(nodeList);
returnstr;
*将yml文件转化为properties文件
*@paramymlFileName工程根目录下(非resources目录)的yml文件名称(如:abc.yml)
*@returnListNode每个Nyml文件中每行对应解析的数据
publicstaticListYmlNodeyamlFile2PropFile(StringymlFileName){
if(ymlFileName==null||ymlFileName.isEmpty()||!ymlFileName.endsWith(".yml")){
thrownewRuntimeException("请输入yml文件名称!!");
FileymlFile=newFile(ymlFileName);
if(!ymlFile.exists()){
thrownewRuntimeException("工程根目录下不存在"+ymlFileName+"文件!!");
StringfileName=ymlFileName.split(".yml",2)[0];
//获取文件数据
Stringyml=read(ymlFile);
ListYmlNodenodeList=getNodeList(yml);
//去掉多余数据,并打印
Stringstr=printNodeList(nodeList);
//将数据写入到properties文件中
StringpropertiesName=fileName+".properties";
Filefile=newFile(propertiesName);
if(!file.exists()){
try{
file.createNewFile();
}catch(IOExceptione){
e.printStackTrace();
try(FileWriterwriter=newFileWriter(file)){
writer.write(str);
writer.flush();
}catch(IOExceptione){
e.printStackTrace();
returnnodeList;
*将yml转化为porperties文件,并获取转化后的Map键值对
*@paramymlFileName工程根目录下的yml文件名称
*@return转化后的porperties文件键值对Map
publicstaticMapString,StringyamlFile2Map(StringymlFileName){
MapString,Stringmap=newHashMap();
ListYmlNodelist=yamlFile2PropFile(ymlFileName);
Strings=printNodeList(list);
String[]lines=s.split(lineSeparator);
Stream.of(lines).forEach(line-{
String[]split=line.split("=");
map.put(split[0],split[1]);
returnmap;
publicstaticMapString,StringyamlStr2Map(Stringyaml){
MapString,Stringmap=newHashMap();
ListYmlNodelist=getNodeList(yaml);
Strings=printNodeList(list);
String[]lines=s.split(lineSeparator);
Stream.of(lines).forEach(line-{
String[]split=line.split("=");
map.put(split[0],split[1]);
returnmap;
privatestaticStringread(Filefile){
if(Objects.isNull(file)||!file.exists()){
return"";
try(FileInputStreamfis=newFileInputStream(file)){
byte[]b=newbyte[(int)file.length()];
fis.read(b);
returnnewString(b,StandardCharsets.UTF_8);
}catch(IOExceptione){
e.printStackTrace();
return"";
privatestaticStringprintNodeList(ListYmlNodenodeList){
StringBuildersb=newStringBuilder();
for(YmlNodenode:nodeList){
if(node.getLast().equals(Boolean.FALSE)){
continue;
if(node.getEmptyLine().equals(Boolean.TRUE)){
sb.append(lineSeparator);
continue;
//判断是否有行级注释
if(node.getHeadRemark().length()0){
Strings="#"+node.getHeadRemark();
sb.append(s).append(lineSeparator);
continue;
//判断是否有行末注释(properties中注释不允许末尾注释,故而放在上面)
if(node.getTailRemark().length()0){
Strings="#"+node.getTailRemark();
sb.append(s).append(lineSeparator);
Stringkv=node.getKey()+"="+node.getValue();
sb.append(kv).append(lineSeparator);
returnsb.toString();
privatestaticListYmlNodegetNodeList(Stringyml){
String[]lines=yml.split(lineSeparator);
ListYmlNodenodeList=newArrayList();
MapInteger,StringkeyMap=newHashMap();
SetStringkeySet=newHashSet();
for(Stringline:lines){
YmlNodenode=getNode(line);
if(node.getKey()!=nullnode.getKey().length()0){
intlevel=node.getLevel();
if(level==0){
keyMap.clear();
keyMap.put(0,node.getKey());
}else{
intparentLevel=level-1;
StringparentKey=keyMap.get(parentLevel);
StringcurrentKey=parentKey+"."+node.getKey();
keyMap.put(level,currentKey);
node.setKey(currentKey);
keySet.add(node.getKey()+".");
nodeList.add(node);
//标识是否最后一级
for(YmlNodeeach:nodeList){
each.setLast(getNodeLast(each.getKey(),keySet));
returnnodeList;
privatestaticbooleangetNodeLast(Stringkey,SetStringkeySet){
if(key.isEmpty()){
returntrue;
key=key+".";
intcount=0;
for(Stringeach:keySet){
if(each.startsWith(key)){
count++;
returncount==1;
privatestaticYmlNodegetNode(Stringline){
YmlNodenode=newYmlNode();
//初始化默认数据(防止NPE)
node.setEffective(Boolean.FALSE);
node.setEmptyLine(Boolean.FALSE);
node.setHeadRemark("");
node.setKey("");
node.setValue("");
node.setTailRemark("");
node.setLast(Boolean.FALSE);
node.setLevel(0);
//空行,不处理
StringtrimStr=line.trim();
if(trimStr.isEmpty()){
node.setEmptyLine(Boolean.TRUE);
returnnode;
//行注释,不处理
if(trimStr.startsWith("#")){
node.setHeadRemark(trimStr.replaceFirst("#","").trim());
returnnode;
//处理值
String[]strs=line.split(":",2);
//拆分后长度为0的,属于异常数据,不做处理
if(strs.length==0){
returnnode;
//获取键
node.setKey(strs[0].trim());
//获取值
Stringvalue;
if(strs.length==2){
value=strs[1];
}else{
value="";
//获取行末备注
StringtailRemark="";
if(value.contains("#")){
String[]vs=value.split("#",2);
if(vs.length==2){
value=vs[0];
tailRemark=vs[1];
node.setTailRemark(tailRemark.trim());
node.setValue(value.trim());
//获取当前层级
intlevel=getNodeLevel(line);
node.setLevel(level);
node.setEffective(Boolean.TRUE);
returnnode;
privatestaticintgetNodeLevel(Stringline){
if(line.trim().isEmpty()){
return0;
char[]chars=line.toCharArray();
intcount=0;
for(charc:chars){
if(c!=''){
break;
count++;
returncount/2;
@Data
classYmlNode{
*层级关系
privateIntegerlevel;
privateStringkey;
privateStringvalue;
*是否为空行
privateBooleanemptyLine;
*当前行是否为有效配置
privateBooleaneffective;
*头部注释(单行注释)
privateStringheadRemark;
*末尾注释
privateStringtailRemark;
*是否为最后一层配置
privateBoole
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 骨科护理技术操作规范
- 大肠息肉术后运动康复护理
- 预见性护理在康复护理中的应用
- 药物应用护理中的持续质量改进
- 内镜间质瘤患者的心理护理与支持
- 南京公务员试题及答案
- 教案-单元六任务1证件照制作-美图秀秀
- (二)安全用电试卷附答案
- 刨花板铺装工核心实操评优考核试卷含答案
- 薪税师合规测试考核试卷含答案
- 2025年下半年安徽省港航集团有限公司所属企业社会公开招聘22名考试参考试题及答案解析
- 安眠药服用安全知识培训课件
- 电机学教案本
- (正式版)DB42∕T 1787.4-2021 《科技馆展览教育通 用要求 第4部分:说明牌》
- 【MOOC答案】《智能仪器设计技术》(东南大学)章节期末慕课答案
- Zippo-2024原版年册完整集合系列
- 盒子记号打印器设计
- 租赁模板脚手架维修保养技术规范
- 《电力管理信息系统工程初步设计文件内容深度规定》编制说明
- TSG G7001-2015 锅炉监督检验规则
- 贵州光伏项目可行性研究报告
评论
0/150
提交评论