kujawk
kujawk

Reputation: 857

Efficient reading/writing of key/value pairs to file in Java

I'm looking for thoughts as to what might be the most efficient way to write/read a large (10,000,000+) set of key/value pairs each consisting of a string of arbitrary length followed by a long integer to/from a file in Java. Any suggestions much appreciated.

Upvotes: 2

Views: 7771

Answers (3)

kujawk
kujawk

Reputation: 857

Using DataInput/OutputStream wrapping a BufferedInput/OuputStream wrapping a FileInput/OutputStream yields acceptable performance for me. Thanks for all the suggestions.

Upvotes: 0

jahroy
jahroy

Reputation: 22692

This is what the Properties API is for:

http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html

Notice that there are methods that operate on InputStreams, OuputStreams, PrintStreams, and different kinds of Readers and Writers.

Upvotes: 2

instanceOfObject
instanceOfObject

Reputation: 2994

Assuming that key and value are separated by some delimiter.

  1. Read whole row using BufferedReader's readLine() method.
  2. Split String by your delimiter and have your map with you!!

This is easiest and extremely efficient (if not most) way!

See commons-io wrapper over it :)

Just don't flush() explicitly, let close() method do it :)

Upvotes: 0

Related Questions