Jump to content

File type suggestions. (Java)

TheKDub

I'm working on a "little" project in java, and I need to have files for individual users, I also need to be able to read from and edit those files at any possible time.

 

I'm thinking about using YAML files, though that can be a pain in the ass with all the different files that I'd have to load in..
I thought about using a .txt file, though scanning through that for a specific line and it's containing values is a pain in the ass with that..

 

I'm considering trying an XML file for it, though unsure of how to write to them and read from them...

 

 

Any other suggestions for the file type I should use? See below for a little "example" of what I need the files to contain, and what I'll use them for.

 

Rank: (RankName)
Banned: True/False

Votes:

 - VotingSite1 month/day/year hour:minute

 - VotingSite2 month/day/year hour:minute
and so on..

 

I'll need the lines to be easily editable, such as changing banned from true to false, or false to true, changing the rank, changing the votes: stuff.

I'll also need to be able to read from the specific lines.

 

Thanks!

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

https://stackoverflow.com/questions/15641666/java-programming-best-way-to-save-read-data-for-an-application

 

The power of Google is infinite.

 

EDIT: If this is a Minecraft server plugin, just use YAML. or MySQL.

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

Why don't you just use a database? It sounds to me to be a more efficient solution.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

https://stackoverflow.com/questions/15641666/java-programming-best-way-to-save-read-data-for-an-application

 

The power of Google is infinite.

 

EDIT: If this is a Minecraft server plugin, just use YAML. or MySQL.

 

It is, and I've searched plenty on google..

 

 

Why don't you just use a database? It sounds to me to be a more efficient solution.

 

If I knew how, I would...

 

Perhaps something such as an excel spreadsheet? or more of an sqlite type of database?

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

It is, and I've searched plenty on google..

 

 

 

If I knew how, I would...

 

Perhaps something such as an excel spreadsheet? or more of an sqlite type of database?

Yeah so since it's for minecraft I'd look at the Spiggot forum (not Bukkit) or just use YAML or a database.

My rig: Intel Core i7-8700K OC 4.8 | NZXT Kraken X62 | ASUS Z370-F | 16 GB Trident Z RGB 3000 (2x8) | EVGA 1070 SC | EVGA SuperNova NEX650G1 | NZXT H700 | Samsung 250GB 850-EVO | 2x 2TB Seagate Barracuda HDDs 

Link to comment
Share on other sites

Link to post
Share on other sites

If I knew how, I would...

 

That's no excuse, there's plenty of resources online about this. Consider a repository pattern, essentially you will want to aim at abstracting your database technology i.e. put the implementation detail behind an interface. This way you can better test/swap it out later.

 

Perhaps something such as an excel spreadsheet?

 

No don't do that.

 

or more of an sqlite type of database?

 

You could, you could even go for a NoSQL option. Just not files... you'll only end up trying to write your own RDBMS.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

You can create a custom data file format which you can define yourself. 

 

Your file could have the following:

 

Start of file:

four byte magic cookie value (to make sure you're not reading an invalid file)

four byte length of each record/person

 

Repeating block:

eight byte Record/person number

one byte Banned boolean

(four byte Number of voting sites for this person, if this is different for each person)

Internal repeating block:

sixy four byte site URL string

sixteen byte String in mm/dd/yyyy hh:mm format

 

End of file

 

All operations could be done with a RandomAccessFile. You could name the file extension whatever you want.

Edit: You'd still have to create your own class to interpret the file, though. 

Edited by Raymondbl
Link to comment
Share on other sites

Link to post
Share on other sites

  • 2 weeks later...

Using a Database to persist data is the best choice you have. If for some reason you do not want a dedicated DB server, you may use an embedded one like H2 or Derby. They store the DB in a file and have a lot of functionality. An embedded DB with JPA (Java Persistence) is a great choice for small portable applications which I've used a lot of times to create small applications (email client, password stores and such) and I can always carry them with me, but please read some tutorials on databases, it will save you a lot of headaches.

If for some reason you really do not want to use a DB, I would do this:

1. Create a POJO (Plain Old Java Class):

public class CharacterData implements Serializable{    private static final long serialVersionUID = 1L;        private int hp;    private int mana;    //constructors + getters and setters}

Serializable interface basically means that this class can be saved locally and then read. It is just a marker. Google for more info.
The serialVersionUID variable is used to check if the object is okay after writing and reading from it. IT IS A MUST for this application.

2. Create a class that will hold all the data:
 

public class ReallyBadDB implements Serializable{    private static final long serialVersionUID = 1L;        private HashMap<String, CharacterData> map = new ... you know the drill;    public void addCharacter(String name, CharacterData data){        map.put(name, data);    }    public CharacterData findCharacter(String name){        return map.get(name);    }}

3. Read and Write to file:

To read and write a file you can just do something like: Create a FileInput(output)Stream from a file and use the readObject and writeObject methods to write the object of type ReallyBadDB that holds all the data.


P.S. A map is used to have a fast way to access data via a key and to make sure you don't have 11^20 characters named Bill (Keys must be unique)
P.S.S. Have fun programming.
    

 

Link to comment
Share on other sites

Link to post
Share on other sites

Using a Database to persist data is the best choice you have. If for some reason you do not want a dedicated DB server, you may use an embedded one like H2 or Derby. They store the DB in a file and have a lot of functionality. An embedded DB with JPA (Java Persistence) is a great choice for small portable applications which I've used a lot of times to create small applications (email client, password stores and such) and I can always carry them with me, but please read some tutorials on databases, it will save you a lot of headaches.

If for some reason you really do not want to use a DB, I would do this:

1. Create a POJO (Plain Old Java Class):

public class CharacterData implements Serializable{    private static final long serialVersionUID = 1L;        private int hp;    private int mana;    //constructors + getters and setters}

Serializable interface basically means that this class can be saved locally and then read. It is just a marker. Google for more info.

The serialVersionUID variable is used to check if the object is okay after writing and reading from it. IT IS A MUST for this application.

2. Create a class that will hold all the data:

 

public class ReallyBadDB implements Serializable{    private static final long serialVersionUID = 1L;        private HashMap<String, CharacterData> map = new ... you know the drill;    public void addCharacter(String name, CharacterData data){        map.put(name, data);    }    public CharacterData findCharacter(String name){        return map.get(name);    }}

3. Read and Write to file:

To read and write a file you can just do something like: Create a FileInput(output)Stream from a file and use the readObject and writeObject methods to write the object of type ReallyBadDB that holds all the data.

P.S. A map is used to have a fast way to access data via a key and to make sure you don't have 11^20 characters named Bill (Keys must be unique)

P.S.S. Have fun programming.

    

 

 

I decided that I was just going to use .YAML for the format as I have a decent idea of how to use it :P

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×