Serializing Multiple Objects in One File in Java

chrstrbrts

Senior member
Aug 12, 2014
522
3
81
Hello,

I'm still learning the finer points of Java. I would like to serialize multiple objects into one file that I can then, in the future, read from.

I know how to serialize one object, but how do I serialize multiple?

I want all of them to be stored in one file. Do I need to use some kind of delimiter to separate the entries?

Also, what extension must that file have? Can I make it a .txt or can I give it any extension?

Thanks.
 
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,615
4,532
75
Why serialize multiple Objects? Why not serialize a single Collection of Objects?
 

chrstrbrts

Senior member
Aug 12, 2014
522
3
81
Why serialize multiple Objects? Why not serialize a single Collection of Objects?

Wait, though. I need to be able to edit that collection.

I can rewrite to a file and erase an old collection and replace it with a collection that reflects the current state of things, yes?
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Hello,

I'm still learning the finer points of Java. I would like to serialize multiple objects into one file that I can then, in the future, read from.

I know how to serialize one object, but how do I serialize multiple?

Ken has this, but yes. Use a Collection and serialize that.

I want all of them to be stored in one file. Do I need to use some kind of delimiter to separate the entries?

If you are worrying too much about what the file looks like and you aren't making a serializer, then you are doing serialization wrong IMO.

Also, what extension must that file have? Can I make it a .txt or can I give it any extension?

It can be any extension. General, though, it is nice to have an extension that matches the serialization format. So, .json, .xml, .yaml, etc. These kinds of extensions provide good hints to future workers to let them know how they can open up the files.


A few tips on serialization in java. Java object serialization is terribly slow. If you can avoid it, don't do it. There are numerous serialization formats that are much, much faster (in fact, java's is about the slowest there is.)

In general, prefer object structures without cyclic references (no path for the a field to get back to the parent) This will make your code cleaner and easier to read while simultaneously making it possible to work with most serializers.

Look into Protobufs and Capnproto. Those are the fastest serialization techniques around, but you MUST start your project working with them, otherwise it is nearly impossible to incorporate them later in the game.

If you want to read it in a text editor, go for json, yaml, or xml. If you don't care about reading, but protobufs sounds too onerous, give FST or Kryo a look.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
Wait, though. I need to be able to edit that collection.

I can rewrite to a file and erase an old collection and replace it with a collection that reflects the current state of things, yes?

Have you considered sqlite? If you are planning on maintaining and rewriting these objects than sqlite might be more convenient to work with.
 

chrstrbrts

Senior member
Aug 12, 2014
522
3
81
If you are worrying too much about what the file looks like and you aren't making a serializer, then you are doing serialization wrong IMO.

I asked about delimiters not because I care about reading the output file. I thought I needed to somehow give the program a way of knowing where one object ends and another begins.

But if I'm using a collection, then I guess that doesn't really matter.

It can be any extension. General, though, it is nice to have an extension that matches the serialization format. So, .json, .xml, .yaml, etc. These kinds of extensions provide good hints to future workers to let them know how they can open up the files.

Hm. I don't know anything about different formats. I have no intention of actually opening or reading the files. I just want a file that can store info for preformed objects so that I can reinstantiate them the next time I run the program.


A few tips on serialization in java. Java object serialization is terribly slow. If you can avoid it, don't do it. There are numerous serialization formats that are much, much faster (in fact, java's is about the slowest there is.)

In general, prefer object structures without cyclic references (no path for the a field to get back to the parent) This will make your code cleaner and easier to read while simultaneously making it possible to work with most serializers.

Look into Protobufs and Capnproto. Those are the fastest serialization techniques around, but you MUST start your project working with them, otherwise it is nearly impossible to incorporate them later in the game.

If you want to read it in a text editor, go for json, yaml, or xml. If you don't care about reading, but protobufs sounds too onerous, give FST or Kryo a look.

I have no idea how to implement anything into a Java program besides Java code. I don't understand how to import these other types of serialization into my program.

Have you considered sqlite? If you are planning on maintaining and rewriting these objects than sqlite might be more convenient to work with.

You mean SQL the database language? Again, I'm way new and can't yet merge to different pieces of code together that way.
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
I asked about delimiters not because I care about reading the output file. I thought I needed to somehow give the program a way of knowing where one object ends and another begins.

But if I'm using a collection, then I guess that doesn't really matter.

Hm. I don't know anything about different formats. I have no intention of actually opening or reading the files. I just want a file that can store info for preformed objects so that I can reinstantiate them the next time I run the program.

I have no idea how to implement anything into a Java program besides Java code. I don't understand how to import these other types of serialization into my program.

My mistake, I should remember that you are a beginner to all this. Sorry about that.

Ok, so as far as default "built into the language" serialization goes, java is the only option, it is terrible (as I mentioned above) but it is easy to work with and get started with. Once you learn how to deal with external libraries, everything I said above should make sense.

Ok, so lets talk about libraries. One of the greatest strengths of java is the fact that it has one of the largest open source communities out there. That means that if you have a problem you are trying to solve, chances are very high that 10 people have already solve the problem 10 different ways so you get the pleasure of picking a solution and using it.

As far as the language goes, Java doesn't really have any sort of external library manager built into the language (bummer) but it does have several decent package managing systems written by other people (yay!).

In general, java has great tools and IDEs. You do yourself a disservice IMO not using them if you aren't already. We use netbeans at my work, but I've heard great things about Intellij and (to a lesser extent) eclipse. I would suggest downloading one of them and start to get familiar with it.

I can't speak for others, but my company uses maven for dependency management (downloading files needed to build and run a program) it works pretty decently and netbeans has really good integration with it. If you go the netbeans maven route (new project, maven project, etc) you can simply add new functionality to your project by adding blocks like so

Code:
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>18.0</version>
</dependency>

Throw these in the "dependencies" block when you hit the clean and build icon maven will automatically go out and download these libraries for you. Alternatively, you can look into setting up and running maven from the command line. (or even looking at other build systems like gradle... though I would stear clear of Ant personally).

Alternatively, you can use a lib folder and direct the java compiler to it while building You can dump all your dependencies into it.

One of the keys to java development is getting comfortable with reading lots and lots of documentation. So get ready to spend a lot of your time in websites that looks like this http://docs.guava-libraries.googlecode.com/git-history/v18.0/javadoc/index.html


You mean SQL the database language? Again, I'm way new and can't yet merge to different pieces of code together that way.

sqlite is a local database that is very fast and pretty simple to work with. It is a sql database, but it is built mainly to be a single user database rather than a database for a whole company.
 

chrstrbrts

Senior member
Aug 12, 2014
522
3
81
My mistake, I should remember that you are a beginner to all this. Sorry about that.

Ok, so as far as default "built into the language" serialization goes, java is the only option, it is terrible (as I mentioned above) but it is easy to work with and get started with. Once you learn how to deal with external libraries, everything I said above should make sense.

Ok, so lets talk about libraries. One of the greatest strengths of java is the fact that it has one of the largest open source communities out there. That means that if you have a problem you are trying to solve, chances are very high that 10 people have already solve the problem 10 different ways so you get the pleasure of picking a solution and using it.

As far as the language goes, Java doesn't really have any sort of external library manager built into the language (bummer) but it does have several decent package managing systems written by other people (yay!).

In general, java has great tools and IDEs. You do yourself a disservice IMO not using them if you aren't already. We use netbeans at my work, but I've heard great things about Intellij and (to a lesser extent) eclipse. I would suggest downloading one of them and start to get familiar with it.

I can't speak for others, but my company uses maven for dependency management (downloading files needed to build and run a program) it works pretty decently and netbeans has really good integration with it. If you go the netbeans maven route (new project, maven project, etc) you can simply add new functionality to your project by adding blocks like so

Code:
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>18.0</version>
</dependency>

Throw these in the "dependencies" block when you hit the clean and build icon maven will automatically go out and download these libraries for you. Alternatively, you can look into setting up and running maven from the command line. (or even looking at other build systems like gradle... though I would stear clear of Ant personally).

Alternatively, you can use a lib folder and direct the java compiler to it while building You can dump all your dependencies into it.

One of the keys to java development is getting comfortable with reading lots and lots of documentation. So get ready to spend a lot of your time in websites that looks like this http://docs.guava-libraries.googlecode.com/git-history/v18.0/javadoc/index.html




sqlite is a local database that is very fast and pretty simple to work with. It is a sql database, but it is built mainly to be a single user database rather than a database for a whole company.

Thanks, but I'm still at the baby step stage. I'm concentrating on getting my first real program written.