Binary File Utils (Free Code)

Hey, everyone. As most of you already know, reading and writing to a binary file is important for larger projects. Binary files are smaller, and read and write faster. However, working with lots of byte arrays can be very annoying. So I made several classes that should help you all make this process much, much easier. The three classes are attached in the paste bin. Here!

Here’s how simple it is to use. Lets say, for example, I have a list of “Person” classes, that I need to store into a file.


public class Person{
    public String name;
    public int age, weight;
    public float height;
    public long phoneNumber;
}

Alright, now let’s write this to a file!


ArrayList<Person> people = (Created somewhere else in code); //The arraylist.

BinaryEncoder bin = new BinaryEncoder();
for(Person p : people){
    bin.addShortString(p.name).addInt(p.age, p.weight).addLong(p.phoneNumber).addFloat(p.height);
}
BinaryFileUtils.writeFile(BinaryFileUtils.getFile("Persons.dat", "Data"), bin.get());

And you’re done. That’s it. No working with complex byte arrays. No measuring, or any math. You’re done.
Reading the file is just as easy!


ArrayList<Person> people = new ArrayList<>();
BinaryDecoder bin = new BinaryDecoder(BinaryFileUtils.readFile(BinaryFileUtils.getFile("Persons.dat", "Data")));
while(!bin.isDone()){
    Person p = new Person();
    people.add(p);
    p.name=bin.getShortString();
    p.age=bin.getInt();
    p.weight=bin.getInt();
    p.phoneNumber=bin.getLong();
    p.height=bin.getFloat();
}

And it’s the exact same list you just wrote into file! Congrats! It’s a simple as that. :slight_smile: Enjoy, and tell me when you guys think.

How is it better than DataOutputStream/DataInputStream?
EDIT: also Files etc.

It’s better because there’s less you have to keep track of. You just call a few commands, and there’s no need to do byte based or binary based math.

Did you even read the things he linked?