This may be done in some cases, but it is error prone. You must be careful about buffers, etc., such as the headers of the ObjectOutputStream stream.
if I want to write a few lines of text into a stream, followed by binary data code or vice versa?
To do this, all you need to know is that you can convert text to binary data and vice versa, but you always need to specify an encoding. However, it is also error prone because people tend to use API methods that use the default encoding for the platform, and of course, you mostly implement a parser for a custom binary file format - a lot can go wrong.
In general, if you are creating a file format, especially when mixing text and binary data, it is better to use the existing infrastructure, for example , Google protocol buffers
source share