Writing (and reading) a mixture of ints and float to (from) a file
in
Programming Questions
•
3 years ago
Hi everyone!
I have a conceptual question on how can I write a mixture of ints and floats to a file? (Reading is also concerned).
Basically, I want to make a savefile for my "game". So I have some classes which in turn have int and float attributes that I want to be stored into a file. And I do not clearly understand how can I organize the saving/loading routines... I am a little bit familiar with FileOutputStream and ObjectOutputStream and DataOutputStream but I have no clear picture on how to organize this task... I have already read many docs on Java and Processing regarding this, but still have no clear view because seems like no one had ever addressed a same problem. For what I know now, it is possble to create a file of the following format:
[int num_of_objects][ints...][floats...]
so that first we know, how many obects are there and how many ints and floats there are to be read... what I would like to know better, is how to write (and read) ints and floats possibly as raw data, so that i can manipulate data on low level.. extracting ints and float from it..
so that i could do something like that (pseudocode here):
-
//////////////////////////WRITING//////////////////////////////bytes[] file_data = new bytes[];
saveFloat(file_data, float my_float); //each value takes 4 bytes i suppose... so also shifting 4 array ///addresses in the routinesaveInt(file_data, int my_int);
writeData(file_data, String filename);
//////////////////////////READING/////////////////////////////bytes[] file_data = ReadFileBytesArray("myfile.dat");
int a = (int) file_data(position); //takes position and converts all from position to position+4 into intsfloat b = (float) file_data(position); //same for floats
...aiming for creating a plain (later also possibly encrypted) data file... from forums i have learnt and implemented a way to store ints as 4 bytes via binary shifts.. but it is such a headache managing all these SIGNED bytes in Java too.. can anyone please give me any directions? Any sensible approach on storing ints and floats to a single file or any ideas on making a savefile with Java would be appreciated..
1