java java中存储永久数据的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3583779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Methods of storing permanent data in java
提问by Yoosuf
I am currently doing a programming assignment and it says that we should "store all the data in a data file and need to check the compatibility of the user PIN and account no. validity"
我目前正在做一个编程作业,它说我们应该“将所有数据存储在一个数据文件中,并需要检查用户 PIN 和帐号有效性的兼容性”
Unfortunately my lecturer has not taught us about data files, and when I googled I found two different answers,
不幸的是,我的讲师没有教我们有关数据文件的知识,当我用谷歌搜索时,我发现了两个不同的答案,
- Store data in notepad (.txt) file
- Store data in csv file
- 将数据存储在记事本 (.txt) 文件中
- 将数据存储在 csv 文件中
MY QUESTION IS WHICH ONE IS A DATA FILE? and how do you retrieve the user PIN (after its typed from buffer reader) to check whether both are correct? Any Help is GREATLY APPRECIATED!!
我的问题是哪个是数据文件?以及如何检索用户 PIN(在从缓冲区读取器输入后)以检查两者是否正确?任何帮助是极大的赞赏!!
回答by Andreas Dolk
The easiestway is using the Propertiesclass. It stores key/value pairs and can persist the data to a properties files. Here's a working example:
在最简单的方法是使用Properties类。它存储键/值对,并且可以将数据保存到属性文件中。这是一个工作示例:
Properties p = new Properties();
p.setProperty("johndoe.pin", "12345");
p.store(new FileWriter("myfile.properties", "");
and reading:
和阅读:
Properties p = new Properties();
p.load(new FileReader("myfile.properties", "");
The check will be done with the properties object:
检查将使用属性对象完成:
public boolean isValid(String user, String pin) {
return pin.equals(p.getProperty(user + ".pin"));
}
It is easy butof course there's no encryption. The file is stored in plain text including the PINs.
这很容易,但当然没有加密。该文件以纯文本形式存储,包括 PIN。
回答by KristofMols
from wikipedia:
来自维基百科:
A data file is a computer file which stores data for use by a computer application or system.
数据文件是存储供计算机应用程序或系统使用的数据的计算机文件。
So both a *.txtfile and a *.csvfile are data files.
所以*.txt文件和*.csv文件都是数据文件。
A *.csv(comma-seperated values) file is in essence a *.txtfile that separates your values by a ,
一个*.csv(逗号分隔的值)文件本质上是一个*.txt文件,它用一个分隔你的值,
The 2 methods you found should do about the same, but it will be easier to use the csv-method.
您找到的 2 种方法应该做的差不多,但使用 csv 方法会更容易。
回答by Hendra Jaya
It doesn't matter whether it's txt or csv. You can even save it in xml. Don't get confused with file extension. The only thing you should care about is "how you can save/load those data".
无论是txt还是csv都没有关系。您甚至可以将其保存在 xml 中。不要与文件扩展名混淆。您唯一应该关心的是“如何保存/加载这些数据”。
- To save in .txt is fine but perhaps you will need to define your own 'formatting' just to make sure that you can load that data.
- If you save it in .csv is also nice since you don't have to invent your own 'formatting'. Just follow several legacy format like delimiter, encloser etc.
- To save in another format is also good. For example, XML. But, again, you need to follow a legacy formatting in XML like DTD.
- 保存在 .txt 中很好,但也许您需要定义自己的“格式”以确保可以加载该数据。
- 如果您将其保存在 .csv 中也很好,因为您不必发明自己的“格式”。只需遵循几种传统格式,如分隔符、封闭符等。
- 以另一种格式保存也不错。例如,XML。但是,同样,您需要遵循 XML 中的遗留格式,如 DTD。
Good luck
祝你好运
回答by jonescb
Serialization is another method of persistent storage.
You can create a class that implements Serializable with a field for each piece of data you want to store. Then you can write the entire class out to a file, and you can read it back in later. I believe the Serializable interface outputs the class into some sort of binary/hex representation.
序列化是另一种持久存储方法。
您可以创建一个实现 Serializable 的类,并为要存储的每条数据提供一个字段。然后你可以把整个类写到一个文件中,你可以稍后再读回来。我相信 Serializable 接口将类输出为某种二进制/十六进制表示。
There are other ways to to do serialization without using the standard Java serialization, like you can use a JSON library.
还有其他方法可以在不使用标准 Java 序列化的情况下进行序列化,就像您可以使用 JSON 库一样。

