-1

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?

Alexanus
  • 679
  • 4
  • 22
  • 2
    Well, if it's only numbers that you need to delimit, I'd argue that a good bet for a delimiter would be a letter. – christopher Apr 12 '15 at 08:36
  • @Alex Yes this solves my problem, lol. Thanks, sorry for the stupid question :), post your comment as answer then i can close this, or should i just delete the question? – Alexanus Apr 12 '15 at 08:36
  • 1
    Why don't you save that in an easy to parse, standard format like JSON or XML, rather than inventing your own format? – JB Nizet Apr 12 '15 at 08:38
  • @JB Nizet, using xml would take longer to save and the file would be bigger, right? – Alexanus Apr 12 '15 at 08:40
  • 1
    @Alexanus So use JSON! – christopher Apr 12 '15 at 08:40
  • ok thanks for the tip, i dont know what it is, but i will read about it. – Alexanus Apr 12 '15 at 08:42
  • Yes, XML is more verbose. JSON is more compact. There are libraries that allow saving in binary format if compactness is such an important feature. But I would choose something simple and easy to read. – JB Nizet Apr 12 '15 at 08:43
  • Why bother with xml or json or anything if just a text file is suitable – Alexander Kulyakhtin Apr 12 '15 at 08:43
  • possible duplicate of [How to save and load a Array in Java](http://stackoverflow.com/questions/11661376/how-to-save-and-load-a-array-in-java) – Joe Apr 12 '15 at 08:52

3 Answers3

2

You should first write the array length followed by the array, in such way you will know how many items to read.

Crazyjavahacking
  • 9,343
  • 2
  • 31
  • 40
0

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

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
-2

You can use this

Writing:

short[] shorts1 = ...; //an array
short[] shorts2 = ...; //another array
long[] longs = ...; //another array
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("the filename"))) {
    out.writeObject(shorts1);
    out.writeObject(shorts2);
    out.writeObject(longs);
    out.flush();
} catch (IOException e) {
    e.printStackTrace();
}

This code first writes two separate short arrays to a file, and after that one long array.


Reading:

short[] shorts1;
short[] shorts2;
long[] longs;
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("the filename"))) {
    shorts1 = (short[])in.readObject();
    shorts2 = (short[])in.readObject();
    longs = (long[])in.readObject();
} catch (IOException | ClassNotFoundException e) {
    e.printStackTrace();
}

This code reads the two separate short arrays and the long array from the file to which they're written.

SWdV
  • 1,715
  • 1
  • 15
  • 36
  • @jenzz Yes it does, use ObjectOutputStream.writeObject (someArray) – SWdV Apr 12 '15 at 09:27
  • Please add an explanation for why this solves the OP's problem, which had to do with reading, not writing, objects. – Nic Apr 19 '15 at 15:11
  • I clarified my answer – SWdV Apr 19 '15 at 16:44
  • Er, no, you didn't. You just gave code, rather than explaining how this answers the question at all. – Nic Apr 19 '15 at 18:54
  • @QPaysTaxes The two short arrays are separated, aren't they? – SWdV Apr 19 '15 at 19:32
  • I don't see what that has to do with anything I said. – Nic Apr 19 '15 at 19:33
  • @QPaysTaxes Alexanus wants to know how to write two separate short arrays to a file (he wants to know when the first array stops and when the second begins, doesn't he?) – SWdV Apr 19 '15 at 19:36
  • So explain -- in your answer, since it's a vital part -- how your code does that. – Nic Apr 19 '15 at 21:24
  • @QPaysTaxes I provided a tiny explanation. Do you understand now? Otherwise I think I don't get you. – SWdV Apr 21 '15 at 14:19