First you need a Paths.class
public class Paths {
private List<String> paths;
public Paths(List<String> paths) {
super();
this.paths = paths;
}
public Paths() {
super();
}
public List<String> getPaths() {
return paths;
}
public void setPaths(List<String> paths) {
this.paths = paths;
}
@Override
public String toString() {
return "Paths [pathList=" + paths + "]";
}
}
and then you write it to your yaml file like this:
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator.Feature;
public class App
{
public static void main(String[] args)
{
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.findAndRegisterModules();
//Writing Values to YAML
//turning off the three Dashes
mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
List<String> values = new ArrayList<>();
values.add("abc");
values.add("def");
values.add("ghi");
Paths paths = new Paths();
paths.setPaths(values);
try {
mapper.writeValue(new File("src/main/resources/pathInput.yaml"), paths);
} catch (IOException e) {
e.printStackTrace();
}
//Reading Values from YAML
try {
Paths path = mapper.readValue(new File("src/main/resources/pathInput.yaml"), Paths.class);
System.out.println(path.getPaths().get(1));
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the second part i read the File as well :) I hope it helps your yaml file should look like this:
paths:
- "abc"
- "def"
- "ghi"
if you need further information read this
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.