[英]Updating values in a yaml file using snakeYaml
Is there any way to edit/delete the values in a YAMLfile using java. 有什么方法可以使用Java 编辑/删除 YAML文件中的值。
I'm using struts2-jquery-grid where the data will be populated from YAML file. 我正在使用struts2-jquery-grid,其中的数据将从YAML文件填充。 Now, If I edit the field(s), it should be saved in YAML file. 现在,如果我编辑字段,则应将其保存在YAML文件中。 I am able to read the value from and write the values to a file in YAML format using yaml.load() and yaml.dump() respectively. 我能够分别使用yaml.load()和yaml.dump()从YAML格式读取值并将其写入文件。 But, I don't have any idea to do edit and delete the specific field. 但是,我不知道要编辑和删除特定字段。
I've gone through http://code.google.com/p/snakeyaml/wiki/Documentation#Dumping_YAML link where I read that we can use template processor and other options like defining order of java bean property, comments to make ease of yaml . 我浏览了http://code.google.com/p/snakeyaml/wiki/Documentation#Dumping_YAML链接,在该链接中,我们了解到我们可以使用模板处理器和其他选项(例如定义Java bean属性的顺序,注释以简化操作) yaml 。 But all those doesn't make any sense to me. 但是所有这些对我来说都没有任何意义。
Here is my code to serialize the list of object called assumption here. 这是我的代码,用于在此处序列化称为假定的对象列表。 In gridModel.add(assumption); 在gridModel.add(assumption);中 , I'm storing the values of class Assumption(bean class with fetters and setters) to the list here in gridModel . ,我将Assumption类(带有束缚器和设置器的bean类)的值存储到gridModel中的列表中。
gridModel.add(assumption);
try {
FileWriter pr=new FileWriter("D:/yaml.yaml");
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
Yaml yaml = new Yaml(options);
String output=yaml.dump(gridModel);//,Tag.MAP, null);
pr.write(output);
System.out.print(pr.toString());
System.out.println();
pr.close();
}catch(Exception e){
e.printStackTrace();
}
The outout I'm getting in a file called yaml.yaml as: 我进入一个名为yaml.yaml的文件,结果是:
For De-serialization the code is: 对于反序列化 ,代码为:
InputStream input = new FileInputStream(new File("D:/yaml.yaml"));
Yaml yaml = new Yaml();
for (Object data : yaml.loadAll(input))
System.out.println("Data:"+data);
The output to the console is : 控制台的输出为:
Data:[Assumption [column1=1, column2=Balwant, column3=SPJ, column4=rerer, column5=null], Assumption [column1=, column2=Vikas, column3=RNC, column4=erer, column5=null], Assumption [column1=3, column2=, column3=PNBE, column4=erer, column5=null], Assumption [column1=4, column2=Diwakar, column3=BGP, column4=rerer, column5=null], Assumption [column1=5, column2=Ajay, column3=Godda, column4=, column5=null]] 数据:[假设[列1 = 1,列2 = Balwant,列3 = SPJ,列4 =错误,列5 =空],假设[列1 =,列2 = Vikas,列3 = RNC,列4 =错误,列5 =空],假设[ column1 = 3,column2 =,column3 = PNBE,column4 = erer,column5 = null],假设[column1 = 4,column2 = Diwakar,column3 = BGP,column4 = rerer,column5 = null],假设[column1 = 5,column2 = Ajay,column3 = Godda,column4 =,column5 = null]]
I'm new to yaml and struts2. 我是yaml和struts2的新手。 So, please answer a clean solution. 因此,请回答一个干净的解决方案。 Correct me, if I'm wrong while describing the requirement. 如果我在描述需求时错了,请纠正我。 Thanks in advance... 提前致谢...
The only way I think to edit is to overwrite the existing yaml. 我认为编辑的唯一方法是覆盖现有的Yaml。 We need to get the values from grid and overwrite ti to the existing one. 我们需要从grid获取值并将ti覆盖为现有值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.