Question

Is there any module in Java equivalent to python's shelve module? I need this to achieve dictionary like taxonomic data access. Dictionary-like taxonomic data access is a powerful way to save Python objects in a persistently easy access database format. I need something for the same purpose but in Java.

Was it helpful?

Solution

I also needed this, so I wrote one. A bit late, but maybe it'll help.

It doesn't implement the close() method, but just use sync() since it only hold the file open when actually writing it.

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;

public class Shelf extends HashMap<String, Object> {
    private static final long serialVersionUID = 7127639025670585367L;
    private final File file;

    public static Shelf open(File file) {
    Shelf shelf = null;
    try {
        if (file.exists()) {
        final FileInputStream fis = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(fis);
        shelf = (Shelf) ois.readObject();
        ois.close();
        fis.close();
        } else {
        shelf = new Shelf(file);
        shelf.sync();
        }
    } catch (Exception e) {
        // TODO: handle errors
    }
    return shelf;
    }

    // Shelf objects can only be created or opened by the Shelf.open method
    private Shelf(File file) {
    this.file = file;
    sync();
    }

    public void sync() {
    try {
        final FileOutputStream fos = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(this);
        oos.close();
        fos.close();
    } catch (Exception e) {
        // TODO: handle errors
    }
    }

    // Simple Test Case
    public static void main(String[] args) {
    Shelf shelf = Shelf.open(new File("test.obj"));
    if (shelf.containsKey("test")) {
        System.out.println(shelf.get("test"));
    } else {
        System.out.println("Creating test string.  Run the program again.");
        shelf.put("test", "Hello Shelf!");
        shelf.sync();
    }
    }
}

OTHER TIPS

You could use a serialisation library like Jackson which serialises POJOs to JSON.

An example from the tutorial:

Jackson's org.codehaus.jackson.map.ObjectMapper "just works" for mapping JSON data into plain old Java objects ("POJOs"). For example, given JSON data

{
  "name" : { "first" : "Joe", "last" : "Sixpack" },
  "gender" : "MALE",
  "verified" : false,
  "userImage" : "Rm9vYmFyIQ=="
}

It takes two lines of Java to turn it into a User instance:

ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
User user = mapper.readValue(new File("user.json"), User.class);

Where the User class looks something like this (from an entry on Tatu's blog):

public class User {
    public enum Gender { MALE, FEMALE };

    public static class Name {
      private String _first, _last;

      public String getFirst() { return _first; }
      public String getLast() { return _last; }

      public void setFirst(String s) { _first = s; }
      public void setLast(String s) { _last = s; }
    }

    private Gender _gender;
    private Name _name;
    private boolean _isVerified;
    private byte[] _userImage;

    public Name getName() { return _name; }
    public boolean isVerified() { return _isVerified; }
    public Gender getGender() { return _gender; }
    public byte[] getUserImage() { return _userImage; }

    public void setName(Name n) { _name = n; }
    public void setVerified(boolean b) { _isVerified = b; }
    public void setGender(Gender g) { _gender = g; }
    public void setUserImage(byte[] b) { _userImage = b; }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top