namespace com.example;
table Person {
name: string;
age: int;
}
root_type Person;
flatc -j person.fbs
import com.example.Person;
import com.google.flatbuffers.FlatBufferBuilder;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public class FlatBuffersPersistenceExample {
public static void main(String[] args) {
FlatBufferBuilder builder = new FlatBufferBuilder();
int nameOffset = builder.createString("John Doe");
int personOffset = Person.createPerson(builder, nameOffset, 30);
builder.finish(personOffset);
ByteBuffer buffer = builder.dataBuffer();
try (FileOutputStream fos = new FileOutputStream("person.dat")) {
fos.write(buffer.array());
} catch (IOException e) {
e.printStackTrace();
}
}
}