1

I'm trying to store the following arrays in binary, and read them back in after:

private int[10] number;
private String[10] name;
private int[10] age;

What is the best way to store them to a binary file, and read them back in afterwards? An example value for each array are:

number[0] = 1; name[0] = "Henry"; age[0] = 35;

So the data belongs sort of together.

Edit: Basically I want to store data from variables (arrays) using a binary text file, binary is a must, and then read the data back in. So in short I want to have a way to store data so when I close the application the data is not lost and can be retrieved each session.

5
  • Can you use like key value pairs but values being separated by comma, like below ? 0=1,Henry,35 Commented Mar 12, 2018 at 18:22
  • Object Serializer? Json? XML? Prob my top 3 in that order. Commented Mar 12, 2018 at 18:25
  • It has to be 3 different arrays? Can you store an object? Commented Mar 12, 2018 at 18:28
  • @royalGhost maybe, it has to be binary though Commented Mar 12, 2018 at 18:35
  • @BernardoRocha yes it has to be 3 different arrays, unless you could combine them in some sort of way as I think you try to suggest by storing an object? I want to use something like a FileWriter or OutputStreamWriter Commented Mar 12, 2018 at 18:37

1 Answer 1

2

You could write custom encoder and decoder e.g. by mapping String to byte[] with String.getBytes() and then writing each byte with OutputStream.write() method. The question is what do you mean by "the best way" because:

  1. Do you need to support different locales? What is the String locale as getBytes() uses platform's default charset.
  2. Do you need to compress the binary data to save space as String might be either very long or very short.
  3. Do you plan to share the binary files with other applications? If so it's best to use a format supported in few languages e.g. Google Protobuf.

If the files are ever to be read outside of your application then Protobuf seems like a sane choice. It would however be an overkill for a Hello World application.

The simpliest approach would be to use ObjectOutputStream and ObjectInputStream but it's far from pretty:

String fileName = "data.set";

try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName))) {
  for (int n : number) {
    out.writeInt(n);
  }
  for (String n : name) {
    out.writeUTF(n);
  }
  for (int a : age) {
    out.writeInt(a);
  }
}

try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName))) {
  for (int i = 0; i < number.length; i++) {
    number[i] = in.readInt();
  }
  for (int i = 0; i < name.length; i++) {
    name[i] = in.readUTF();
  }
  for (int i = 0; i < age.length; i++) {
    age[i] = in.readInt();
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

The binary files won't be shared with other applications. It's just used to save variables to a text file (binary is a must) and read back from them so when I close the application, the data is not lost.
I'd go with Protobuf if it's not a Hello World application. It's straightforward. Just wrap your data into a type: it looks like you can create new Person[10] with number, name and age being Person fields. Then create a .proto file, generate the Java classes from the .proto file and use it as show here. Alternatively you can use standard Java serialization using ObjectInputStream/ObjectOutputStream but you still need a wrapper class for your data.
@Walt added an example of Java serialization

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.