Java如何使用Jackson、SnakeYAML或YamlBeans库的API创建YAML文件
使用Jackson库创建YAML文件,你需要添加以下依赖到你的maven项目中:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.12.5</version>
</dependency>
以下是一个YAML文件的样例:
yaml
users:
- name: John
age: 25
- name: Jane
age: 30
使用Jackson库创建这个YAML文件的示例代码如下:
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
User user1 = new User("John", 25);
User user2 = new User("Jane", 30);
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
UsersWrapper wrapper = new UsersWrapper(users);
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
try {
mapper.writeValue(new File("path/to/output.yaml"), wrapper);
} catch (IOException e) {
e.printStackTrace();
}
}
static class User {
private String name;
private int age;
public User() {}
public User(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
static class UsersWrapper {
@JsonProperty("users")
private List<User> users;
public UsersWrapper() {}
public UsersWrapper(List<User> users) {
this.users = users;
}
// Getters and setters
}
}
使用SnakeYAML库创建YAML文件,你需要添加以下依赖到你的maven项目中:
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.28</version>
</dependency>
以下是一个YAML文件的样例(同上)。
使用SnakeYAML库创建这个YAML文件的示例代码如下:
import org.yaml.snakeyaml.Yaml;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
public class Main {
public static void main(String[] args) {
User user1 = new User("John", 25);
User user2 = new User("Jane", 30);
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
Map<String, List<User>> data = new LinkedHashMap<>();
data.put("users", users);
Yaml yaml = new Yaml();
try {
FileWriter writer = new FileWriter("path/to/output.yaml");
yaml.dump(data, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
static class User {
private String name;
private int age;
public User() {}
public User(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
}
使用YamlBeans库创建YAML文件,你需要添加以下依赖到你的maven项目中:
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>yamlbeans</artifactId>
<version>1.14</version>
</dependency>
以下是一个YAML文件的样例(同上)。
使用YamlBeans库创建这个YAML文件的示例代码如下:
import com.esotericsoftware.yamlbeans.YamlWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
User user1 = new User("John", 25);
User user2 = new User("Jane", 30);
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
UsersWrapper wrapper = new UsersWrapper(users);
try {
FileWriter writer = new FileWriter("path/to/output.yaml");
YamlWriter yamlWriter = new YamlWriter(writer);
yamlWriter.write(wrapper);
yamlWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
static class User {
private String name;
private int age;
public User() {}
public User(String name, int age) {
this.name = name;
this.age = age;
}
// Getters and setters
}
static class UsersWrapper {
private List<User> users;
public UsersWrapper() {}
public UsersWrapper(List<User> users) {
this.users = users;
}
// Getters and setters
}
}
请将代码中的`path/to/output.yaml`替换为你想要输出到的文件路径。