0

Can you use Java's built in object serialization with a random access file? My motivation is my answer to this question.

Community
  • 1
  • 1
M.J. Rayburn
  • 509
  • 4
  • 14
  • There is nothing 'too broad' about this question. It has two specific yes/no answers, and where the answer is 'yes' there is a limited range of techniques. Ridiculous closure votes and downvotes here. – user207421 Dec 12 '14 at 02:42
  • 1
    It's nothing more than a straw-man question to allow the original poster to simultaneously post ridiculous answers. At least bkail posted an answer that has some merit. – Hovercraft Full Of Eels Dec 12 '14 at 03:34
  • @HovercraftFullOfEels That may or may not be so, but it doesn't make it 'too broad'. – user207421 Dec 12 '14 at 08:51

2 Answers2

2

You could use serialization with a random access file, but you'll have to build the infrastructure yourself. There is no way to get the size of serialization without actually serializing.

I assume you want to store multiple serialized objects in the same file, so you'll need to do something like:

  1. Store the serialized objects, keeping track of the offsets within the stream until the end, and then write a table of contents at the end of the file. This is similar to the zip file format.

-or-

  1. Write "placeholder" bytes for the size, then serialize the object to the stream, then seek back to the placeholder and write the number of bytes actually written.
Brett Kail
  • 33,593
  • 2
  • 85
  • 90
-1

You could (using java's ByteArrayOutputStream and ByteArrayInputStream), but it would be a very bad idea. Java's built in object serialization isn't memoryless. "The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written." You could get around this by creating a new instance of ObjectOutputStream/ObjectInputStream for each object you write/read, but that would make your file excessively large.

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

class Test {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);

        objectOutputStream.writeObject(1);
        System.out.println(byteArrayOutputStream.size());
        byteArrayOutputStream.reset();

        objectOutputStream.writeObject(2);
        System.out.println(byteArrayOutputStream.size());
        byteArrayOutputStream.reset();

        objectOutputStream.writeObject(2);
        System.out.println(byteArrayOutputStream.size());
        byteArrayOutputStream.reset();

        objectOutputStream.writeObject(3);
        System.out.println(byteArrayOutputStream.size());
        byteArrayOutputStream.reset();

        objectOutputStream.close();
    }
}

Output

  • 81
  • 10
  • 5
  • 10
M.J. Rayburn
  • 509
  • 4
  • 14
  • Thats a rather short answer, to add to this: you can wrap a ByteArrayOutputStream into a ObjectOutputStream. With this construct you can serialize your object to byte[]s. Now you can use any encoding you want to stuff those byte arrays into the file. And the size of the array is the one you care about. – eckes Dec 12 '14 at 21:58