Java is the fastest and fastest way to read and store data

I am using java (LWJGL) to create a kind of game and I want to know the fastest way to read and store data on hard disk. The game world is sort of a grid representing the 3D world and I need to store data about each 1x1x1 cube (or maybe lower in the future to get a better resolution). Currently I am saving each cube record to a text file. Each record stores data for a 32x32x32 cube.

Currently, each write takes 625KB on the hard drive, and it takes 16 milliseconds to read the file (on my computer, no other stuff, just reading it).

I wonder if there is a better way to do this, because I could use this method every 3 seconds (in most cases it won't, but sometimes it will - need to keep the 60 frame rate). Path to the same data, so it will consume less memory and read faster. Text files only use numbers, English letters and some special characters (',' '(' ')' '{' '}' '-' '.' ':' And maybe a little more).

I heard that minecraft uses .mca files, but I have no idea how much better they are or how to read them.

+3


source to share


2 answers


One way to do what you want is to use serialization and object streams to store binary data in a file. Basically, you would keep your array object or chunk object (or whatever you name your terrain subdivisions). This would provide faster load times since the JVM would not have to rebuild every object on load.

The main advantage of using binaries is that you don't have to write any algorithm or even iterate over the data when you want to store it. As you mentioned that you will want to record frequently, this might be a good solution for you.

Here's an example to give you some pointers.

Let's assume:

class Chunk implements java.io.Serializable {

    private static final long serialVersionUID = 1L;

    Block[][][] chunkBlocks;
    // ... other code //
}

      

by adding a statement implements Serializable

to the class definition, you mean that the class can be used to write and read objects from byte streams, which allows you to store them and read them from files. Another note is variable serialVersionUID

. This is very important because this is what will tell the thread what class this object belongs to, and if it can be applied to it. (note: this is a rough approximation, but I'll spare you the details for the sake of not being too detailed). Modern IDEs like eclipse or IntelliJ will prompt you to add this variable to your class when implementing the Serializable interface. They will also give you the option to create a unique identifier for it. This is recommended to prevent conflicts.

finally, this is how you are going to store the object:



/**
 * Object o         : the object you want to save
 * String filename  : the file path of where you want to save the object
 */
public static void save(Object o, String filename) {

    FileOutputStream fos = null;
    ObjectOutputStream oop = null;

    try {
        fos = new FileOutputStream(filename);
        oop = new ObjectOutputStream(fos);
        oop.writeObject(o);
        oop.flush();
        fos.flush();
        oop.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

      

and download one:

/**
 * @returns Object o : the object stored in the file @ filename
 * String filename   : the file path of where you want to save the object
 */
public static Object load(String filename) {

    FileInputStream in;
    ObjectInputStream ois;

    Object o = null;

    try {
        in = new FileInputStream(filename);
        ois = new ObjectInputStream(in);
        o = ois.readObject();
        ois.close();
        in.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
   }

   return o;

}

      

In the code above, I've summarized the functions to be taken and returned java.lang.Object

, but you can change that to Chunk or whatever if you want to have a function targeting a specific class.

You also specify Minecraft file formats, but they are very application specific and basically have a format that makes sense for him and only him. In addition to this, they are not suitable for frequent saving as you would like to do, they are better at unloading the background.

Hope this helped!

0


source


The absolute fastest way to access a fixed size file of the type you mention is via NIO MappedByeBuffer.



0


source







All Articles