话不多说直接上代码
package ram;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import java.io.*;
public class Main {
//存放json数据
public static void main(String[] args) throws IOException {
//输出路径
String path = "D:\\WangZY\\data\\data1.json";
//实例化userall 自己写的一个userall方法 代码在下方
UserAll userAll = new UserAll();
//路径
File file = new File(path);
//判断路径是否存在,不存在就创建
if(!file.getParentFile().exists()){
file.getParentFile().mkdir();
}
file.createNewFile();
//写入的路径 和 编码格式
Writer writer = new OutputStreamWriter(new FileOutputStream(file),"UTF-8");
//循环输出XXX条数据随意改(输出10000条json数据)
for (int i = 1; i <=10000 ; i++) {
//调用userall中的添加数据方法 User自己写的类 代码在下方
User user = userAll.getUserAll();
//(01) 标准json格式 建议用下方的(02)
//String userData = JSON.toJSONString(user,SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue, SerializerFeature.WriteDateUseDateFormat);
//拿到数据 (02)
String userData = JSON.toJSONString(user);
//将数据写入
writer.write(userData+"\n");
System.out.println(Main.class+"输出了:"+"\t"+i+"条数据!!!");
}
writer.flush();
writer.close();
}
}
user类代码
就是定义几个json需要的元素
package ram;
public class User {
private String id;//id
private String name;//姓名
private int age; // 年龄
private String sex;//性别
private String province; //省份
private String city; //城市
private String pione;//手机号
private String profession; //职业
private String salary; //工资
private String hobby; //爱好
private String date; //访问时间
public User() {
}
public User(String id, String name, int age, String sex, String province, String city, String pione, String profession, String salary, String hobby, String date) {
this.id = id;
this.name = name;
this.age = age;
this.sex = sex;
this.province = province;
this.city = city;
this.pione = pione;
this.profession = profession;
this.salary = salary;
this.hobby = hobby;
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPione() {
return pione;
}
public void setPione(String pione) {
this.pione = pione;
}
public String getProfession() {
return profession;
}
public void setProfession(String profession) {
this.profession = profession;
}
public String getSalary() {
return salary;
}
public void setSalary(String salary) {
this.salary = salary;
}
public String getHobby() {
return hobby;
}
public void setHobby(String hobby) {
this.hobby = hobby;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
}
UserAll的代码
这里面的赋值我都是自己写的随机方法(随机姓名,随机id,随机性别··············)
package ram;
import org.apache.commons.lang.RandomStringUtils;
import static ram.RandomTest.*;//自己写的一个随机类
import static ram.SiteAll.place;//自己洗的一个随机城市方法
public class UserAll {
//添加信息
public static User getUserAll(){
User user = new User();
//RandomTest 是一个自写的随机的类
//设置id
user.setId(RandomStringUtils.random(6,"1234567890"));
//设置姓名 赋值我全是调用的自己写的随机方法 你们可以直接写值比如 ·· user.setName("自己随意就好")
user.setName(RandomTest.RandomName());
//设置年龄
user.setAge(RandomTest.getUserAge());
//设置性别
user.setSex(RandomTest.getUserSex()==1?"男":"女");
//设置省份
String[] string = place[getUserRandom(place.length)];
user.setProvince(string[0]);
//设置城市
user.setCity(string[(getUserRandom((string.length)))]);
//设置手机号
user.setPione(RandomTest.getUserPhone());
//设置职业
String zhiYe = (getUserZhiYe()==1?"打野":getUserZhiYe()==2?
"法师":getUserZhiYe()==3?"射手":getUserZhiYe()==4?"辅助":"上单");
user.setProfession(zhiYe);
//设置工资
user.setSalary(RandomTest.getUserSalary());
//设置爱好
String hobby = (getUserHobby()==1?"打球":getUserHobby()==2?"打游戏":getUserHobby()==3?"看电影":
getUserHobby()==4?"玩手机":getUserHobby()==5?"吃零食":getUserHobby()==6?"购物":
getUserHobby()==7?"看女人":getUserHobby()==8?"睡觉":getUserHobby()==9?"学习":"炸街");
user.setHobby(hobby);
//设置时间
user.setDate(RandomTest.getUserDate());
return user;
}
}
自己去写的路径里找输出的文件
给你们看一下结果
感觉好理解的伙伴们 记得给点个赞哦!!!
谢谢观看!!