简体   繁体   English

Java数据存储方法

[英]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. 我正在考虑完全用本机java编写这个,我正在使用文本和属性文件来完成我的大部分存储。

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? 现在你可能在想,为什么他不只是使用arraylist,或只是将数据存储在另一个文本文件中? 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: 据我所知,有一个arraylist我不能存储整数,我想用以下命令查询数据:

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. 再一次,我不想最终将它存储在MySQL中并且必须使用JDBC驱动程序,我希望整个应用程序在本机java中运行。

You can also use XML files to structure your data properly. 您还可以使用XML文件正确构建数据。 When it comes to querying your data, you can make use of XPath. 在查询数据时,您可以使用XPath。

XML will give you the chance to allow for multiple data formats, including numbers and all. XML将为您提供允许多种数据格式的机会,包括数字和所有数据格式。

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? 使用XPath查询数据:单词翻译的成本是多少?

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

Then use the result to divide by 100 for the price of a word. 然后使用结果除以100为单词的价格。

Manipulating XML should be a walk in the park with a library such as JDOM2. 操作XML应该是一个带有JDOM2等库的公园。

Just create an object called TranslationType of something to model a record. 只需创建一个名为TranslationType的对象来建模记录。 It will have properties for id, language,... 它将具有id,语言,...的属性

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. 潜在的持久性实现:简单的文本文件或二进制文件/序列化或JDBC(其他数据库类型)等。

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. 您还可以使用带有id作为键的Map (HashMap)和自定义对象Translation作为包含所有行数据的值。 Make the object implement Serializable and serialize your map to disk. 使对象实现Serializable并将映射Serializable化为磁盘。 HashMap already implements Serializable . HashMap已经实现了Serializable

Who says you have to use MySQL? 谁说你必须使用MySQL? You can use SQLite for this. 您可以使用SQLite。 There are JDBC adapters for it that run on every major OS. 它有适用于它的JDBC适配器,可在每个主要操作系统上运行。

Whats wrong with putting them on a single line? 将它们放在一条线上有什么不对? Search for the line containing <ID> in the file named <integer> .txt 在名为<integer> .txt的文件中搜索包含<ID>的行

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? 然后你可以编写你的代码来搜索以这么多单词的数量命名的文件,然后在那个文件中搜索你想要的特定ID?

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? 会找到一个名为1000w.txt的文件并获取包含该ID的行,然后解析它的总数?

One good solution is to store the data as XML files. 一个好的解决方案是将数据存储为XML文件。 Create and Define XML Schemas for the data file and data to be stored for your problem in this case it is 在这种情况下,为您的问题创建和定义数据文件和数据的XML模式

ID Language from Language to Cost/100w Comment 7101 English Mandarin $5.00 Outsource ID语言从语言到成本/ 100w评论7101英语普通话$ 5.00外包

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. 您可以选择使用JAX-B或XML-Beans库从定义的模式生成类类型。 Read the XML data unmarshall and marshal either stream or objects as per the application's requirement. 根据应用程序的要求,读取XML数据unmarshall并封送流或对象。

you can most def store int datatype in an array. 你可以在数组中大多数def存储int数据类型。 just an example: 只是一个例子:

    int[] arrayname = new int[n]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM