java中存储永久数据的方法
我目前正在做一项编程作业,它说我们应该 “将所有数据存储在数据文件中,需要检查用户PIN和帐号有效性的兼容性”
不幸的是,我的讲师没有教我们有关数据文件的知识,当我用谷歌搜索时,我发现了两个不同的答案,
- 将数据存储在记事本中(.txt) 文件
- 将数据存储在 csv 文件中
我的问题是哪一个是数据文件?以及如何检索用户 PIN(从缓冲区读取器输入后)以检查两者是否正确? 非常感谢任何帮助!
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"
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
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!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法是使用
Properties
类。它存储键/值对,并可以将数据保存到属性文件中。这是一个工作示例:并阅读:
检查将使用属性对象完成:
这很容易但是当然没有加密。该文件以纯文本形式存储,包括 PIN。
The easiest way is using the
Properties
class. It stores key/value pairs and can persist the data to a properties files. Here's a working example:and reading:
The check will be done with the properties object:
It is easy but of course there's no encryption. The file is stored in plain text including the PINs.
来自维基百科:
因此,
*.txt
文件和*.csv
文件都是数据文件。*.csv
(逗号分隔值)文件本质上是一个*.txt
文件,它通过,
分隔您的值。两种方法您发现应该做同样的事情,但使用 csv 方法会更容易。
from wikipedia:
So both a
*.txt
file and a*.csv
file are data files.A
*.csv
(comma-seperated values) file is in essence a*.txt
file that separates your values by a,
The 2 methods you found should do about the same, but it will be easier to use the csv-method.
不管是txt还是csv都没关系。您甚至可以将其保存为 xml。
不要与文件扩展名混淆。您唯一应该关心的是“如何保存/加载这些数据”。
祝你好运
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".
Good luck
序列化是持久存储的另一种方法。
您可以创建一个实现 Serialized 的类,并为要存储的每条数据设置一个字段。然后,您可以将整个类写入一个文件,稍后可以将其读回。我相信可序列化接口将类输出为某种二进制/十六进制表示形式。
还有其他方法可以在不使用标准 Java 序列化的情况下进行序列化,例如可以使用 JSON 库。
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.
There are other ways to to do serialization without using the standard Java serialization, like you can use a JSON library.