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. 运行结果,成功写入文件
![运行结果](https://i-blog.csdnimg.cn/blog_migrate/2d4d78f5ff3d6f03a5071751c1bd94dc.png#pic_center)
![a](https://i-blog.csdnimg.cn/blog_migrate/9568f42dbf700903f385f0f0bf24b8d5.png#pic_center)