1. 创建实例类
package com.fy.test;
public class Student {
private int id;
private String name;
private boolean sex;
public Student(int id, String name, boolean sex) {
this.id = id;
this.name = name;
this.sex = sex;
}
public Student() {
}
public Student(int i, String zhu) {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
}
实现类
package com.fy.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.lang.management.ManagementPermission;
public class TestJackson {
@Test
public void f() throws Exception {
ObjectMapper mapper = new ObjectMapper();
Student student = new Student(12,"zhu",true);
mapper.writeValue(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\b.txt"), student);
mapper.writeValue(new FileWriter("C:\\Users\\Administrator\\Desktop\\a.txt"),student);
}
}
3. 运行结果,成功写入文件