Protobufs don't have much in common with a line-separated text file. Protobuf is used to break objects into bytes. This process is called serialization. Protobuf is especially focused on compatibility, and small size.
The problem you're having is protobufs do not store information about how many bytes each object is composed of, or what type each object is. So, if you store many protobuf serialized objects to a file, you can't extract them without including data about what type of object is to follow, and how many bytes that object is made of.
This data is referred to as a header.
public void serializeProtobufObject(OutputStream stream, Object obj){
byte[] bytes = getProtobufBytes(obj);
int id = getObjectID(obj);
//write protobuf header info
writeInt(stream,id);
writeInt(stream,bytes.length);
//write protobuf payload
stream.write(bytes,0,bytes.length);
}
//called repeatedly for many objects in the same stream.
public Object deserializeProtobufObject(InputStream stream){
//read protobuf header
int id = readInt(stream);
int length = readInt(stream);
//use header to interpret payload
return readObject(id, length, stream);
}
An integer ID will tell you what type of object is following. An integer length tells you how many bytes the object is composed of. When you deserialize, you'll use these 2 pieces of information to extract the protobuf object. You'll do this repeatedly if you've many protobuf objects in the same stream.
A superior approach here would be to create a Protobuf object for these 2 fields and serialize objects like so to your stream:
ProtobufHeader for Foo
[Foo]
ProtobufHeader for Bar
[Bar]
This would allow you to expand your protobuf header in the future.