Download address

Github.com/protocolbuf…

Go to the release repository and select the latest package to download (I don’t care, I want the latest package)

The installation

CD protobuf-3.17.3 cp -r include /usr/local/include/cp -r bin /usr/local/bin/Copy the code

There you go. It’s so smooth

use

UserPB.info

syntax = "proto3";

option java_outer_classname = "UserPB";
option java_package = "com.jiuling.resource.proto";

message info{
    string addr = 1;    / / address
    string group = 2;   / / group
}
Copy the code

ProtoBufTest

 public static void main(String[] args) {

        UserPB.info info = UserPB.info.newBuilder().setAddr("Test").setGroup("group1").build();

        System.out.println(info);
        byte[] data = info.toByteArray();
        try {
            UserPB.info p2 = UserPB.info.parseFrom(data);
            System.out.println(p2);
        } catch(InvalidProtocolBufferException e) { e.printStackTrace(); }}Copy the code

Output result:

Handling Chinese exceptions

 public static void main(String[] args) {

        UserPB.info info = UserPB.info.newBuilder().setAddr("Test").setGroup("group1").build();
        System.out.println(TextFormat.printer().escapingNonAscii(false).printToString(info));

        System.out.println(info.getAddr());
// System.out.println(info);
        byte[] data = info.toByteArray();
        try {
            UserPB.info p2 = UserPB.info.parseFrom(data);
            System.out.println(TextFormat.printer().escapingNonAscii(false).printToString(p2));
            System.out.println(p2.getAddr());
        } catch(InvalidProtocolBufferException e) { e.printStackTrace(); }}Copy the code

done

Basically is to record some, very basic, but you want to search, very vexed base hold ~