Alexanus Alexanus - 19 days ago 6
Java Question

Save diffrent kind of variables to a file

I want to save diffrent kind of variables to a file. Currently I am using

DataOutputStream
to do so. First i save an array of shorts, then i save another array of shorts, then i want to save an array of longs. The problem is when i read the file, I dont know how many shorts i saved. I dont know how big the first short array is.

I could solve this by specifying a value that let me know when the short array stops. For example i say the value -99999 tells me when the array ended. But there is a small chance the short array contains the value -99999 before it ended.

Is there a way to create marks? Or should I create diffrent files for each array?

Answer

Read the length of the array first, then read that many elements

Comments