user1080390
user1080390

Reputation: 471

Java data storage methods

I'm currently writing a client-server application to allow the client to request data from the server. I'm looking at writing this entirely in native java, and i'm using text and property files to do the bulk of my storage.

However, I've hit a brick wall trying to find a good way to store the following:

ID    Language from    Language to    Cost/100w    Comment
7101    English    Mandarin    $5.00    Outsource

Now you may be thinking, why doesn't he just use an arraylist, or just store the data in another text file? I've considered these options and I see downfalls with each.

With an arraylist to my knowledge I cannot store integers, and I'd like to query the data with the following command:

What is the cost of <ID> translation of <integer> words?

As you can see i'll require the cost to be stored as an integer so I can use it for sums.

With text files I only know how to store the data on a single line, for example:

7101 English Mandarin $5.00 Outsource
7102 Greek Russian $12.00 -
etc. etc. etc. etc. etc

So I don't know how I could query the data.

Once again, I do not want to end up storing this with MySQL and having to use the JDBC driver, I want the entire application to run in native java.

Upvotes: 2

Views: 3018

Answers (7)

TuckerB
TuckerB

Reputation: 3

you can most def store int datatype in an array. just an example:

    int[] arrayname = new int[n]

Upvotes: 0

user1054394
user1054394

Reputation:

You can also use XML files to structure your data properly. When it comes to querying your data, you can make use of XPath.

XML will give you the chance to allow for multiple data formats, including numbers and all.

For example:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <row id="7101">
        <language-from>English</language-from>
        <language-to>Mandarin</language-to>
        <cost>5.00</cost>
        <comment><![CDATA[Some comment]]></comment>
    </row>
</data>

To query the data using XPath: What is the cost of translation of words?

/data/row[@id=7101]/cost/number()

Then use the result to divide by 100 for the price of a word.

Manipulating XML should be a walk in the park with a library such as JDOM2.

Upvotes: 5

dosakiller
dosakiller

Reputation: 79

One good solution is to store the data as XML files. Create and Define XML Schemas for the data file and data to be stored for your problem in this case it is

ID Language from Language to Cost/100w Comment 7101 English Mandarin $5.00 Outsource

Defining a schema for the data file gives you more control and enforce well defined types applicable to the problem statement.

You can choose to use JAX-B or XML-Beans library to generate class types from the defined schema. Read the XML data unmarshall and marshal either stream or objects as per the application's requirement.

Upvotes: 0

dcernahoschi
dcernahoschi

Reputation: 15250

You can also use a Map (HashMap) with your id as keys and a custom object Translation as value that contains all your line data. Make the object implement Serializable and serialize your map to disk. HashMap already implements Serializable.

Upvotes: 2

WebDevNewbie
WebDevNewbie

Reputation: 1863

Whats wrong with putting them on a single line? Search for the line containing <ID> in the file named <integer>.txt

Then you could write your code to search for the file named after the amount per so many words, then search that file for the specific ID you want?

What is the cost of <7101> translation of <1000> words? Would find a file named 1000w.txt and grab the line containing that ID, then parse it for the total?

Upvotes: 1

Dan D.
Dan D.

Reputation: 32391

Just create an object called TranslationType of something to model a record. It will have properties for id, language,...

Then you can define several persistence types, hidden behind some smart interfaces such as you can exchange the used persistence implementation whenever you want, even at runtime. Potential persistence implementations: simple text files or binary files / serialization or JDBC (other DB types), and so on.

Upvotes: 3

Mike Thomsen
Mike Thomsen

Reputation: 37526

Who says you have to use MySQL? You can use SQLite for this. There are JDBC adapters for it that run on every major OS.

Upvotes: 1

Related Questions