option java_package = "com.shun";
option java_outer_classname = "StudentProto";
message Student {
required int32 id = 1;
optional string name = 2;
optional int32 age = 3;
}</span>
import "student.proto";
option java_package = "com.shun";
option java_outer_classname = "TeacherProto";
message Teacher {
required int32 id = 1;
optional string name = 2;
repeated Student student_list = 3;
}</span>
protoc --java_out=D:protobufferVsJsonsrc D:protoFilesteacher.proto D:protoFilesstudent.proto
<span style="font-size: 16px;">protoc --java_out=还是上面的放代码的地方 proto文件的路径(这里是descriptor.proto文件的路径)</span>
package com.shun.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.shun.StudentProto.Student;
import com.shun.TeacherProto.Teacher;
public class ProtoWriteTest {
public static void main(String[] args) throws IOException {
Student.Builder stuBuilder = Student.newBuilder();
stuBuilder.setAge(25);
stuBuilder.setId(11);
stuBuilder.setName("shun");
//构造List
List<Student> stuBuilderList = new ArrayList<Student>();
stuBuilderList.add(stuBuilder.build());
Teacher.Builder teaBuilder = Teacher.newBuilder();
teaBuilder.setId(1);
teaBuilder.setName("testTea");
teaBuilder.addAllStudentList(stuBuilderList);
//把gpb写入到文件
FileOutputStream fos = new FileOutputStream("C:\Users\shun\Desktop\test\test.protoout");
teaBuilder.build().writeTo(fos);
fos.close();
}
}</span>
package com.shun.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import com.shun.StudentProto.Student;
import com.shun.TeacherProto.Teacher;
public class ProtoReadTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
Teacher teacher = Teacher.parseFrom(new FileInputStream("C:\Users\shun\Desktop\test\test.protoout"));
System.out.println("Teacher ID:" + teacher.getId() + ",Name:" + teacher.getName());
for (Student stu:teacher.getStudentListList()) {
System.out.println("Student ID:" + stu.getId() + ",Name:" + stu.getName() + ",Age:" + stu.getAge());
}
}
}</span>
package com.shun.test;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.shun.Student;
import com.shun.Teacher;
public class GsonWriteTest {
public static void main(String[] args) throws IOException {
Student stu = new Student();
stu.setAge(25);
stu.setId(22);
stu.setName("shun");
List<Student> stuList = new ArrayList<Student>();
stuList.add(stu);
Teacher teacher = new Teacher();
teacher.setId(22);
teacher.setName("shun");
teacher.setStuList(stuList);
String result = new Gson().toJson(teacher);
FileWriter fw = new FileWriter("C:\Users\shun\Desktop\test\json");
fw.write(result);
fw.close();
}
}</span>
package com.shun.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.shun.StudentProto.Student;
import com.shun.TeacherProto.Teacher;
public class ProtoWriteTest {
public static final int SIZE = 100;
public static void main(String[] args) throws IOException {
//构造List
List<Student> stuBuilderList = new ArrayList<Student>();
for (int i = 0; i < SIZE; i ++) {
Student.Builder stuBuilder = Student.newBuilder();
stuBuilder.setAge(25);
stuBuilder.setId(11);
stuBuilder.setName("shun");
stuBuilderList.add(stuBuilder.build());
}
Teacher.Builder teaBuilder = Teacher.newBuilder();
teaBuilder.setId(1);
teaBuilder.setName("testTea");
teaBuilder.addAllStudentList(stuBuilderList);
//把gpb写入到文件
FileOutputStream fos = new FileOutputStream("C:\Users\shun\Desktop\test\proto-" + SIZE);
teaBuilder.build().writeTo(fos);
fos.close();
}
}</span>
package com.shun.test;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.google.gson.Gson;
import com.shun.Student;
import com.shun.Teacher;
public class GsonWriteTest {
public static final int SIZE = 100;
public static void main(String[] args) throws IOException {
List<Student> stuList = new ArrayList<Student>();
for (int i = 0; i < SIZE; i ++) {
Student stu = new Student();
stu.setAge(25);
stu.setId(22);
stu.setName("shun");
stuList.add(stu);
}
Teacher teacher = new Teacher();
teacher.setId(22);
teacher.setName("shun");
teacher.setStuList(stuList);
String result = new Gson().toJson(teacher);
FileWriter fw = new FileWriter("C:\Users\shun\Desktop\test\json" + SIZE);
fw.write(result);
fw.close();
}
}</span>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有