java中存储永久数据的方法

发布于 2024-09-15 22:54:25 字数 248 浏览 5 评论 0原文

我目前正在做一项编程作业,它说我们应该 “将所有数据存储在数据文件中,需要检查用户PIN和帐号有效性的兼容性”

不幸的是,我的讲师没有教我们有关数据文件的知识,当我用谷歌搜索时,我发现了两个不同的答案,

  1. 将数据存储在记事本中(.txt) 文件
  2. 将数据存储在 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,

  1. Store data in notepad (.txt) file
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

千笙结 2024-09-22 22:54:25

最简单的方法是使用Properties类。它存储键/值对,并可以将数据保存到属性文件中。这是一个工作示例:

Properties p = new Properties();
p.setProperty("johndoe.pin", "12345");
p.store(new FileWriter("myfile.properties", "");

并阅读:

Properties p = new Properties();
p.load(new FileReader("myfile.properties"), "");

检查将使用属性对象完成:

public boolean isValid(String user, String pin) {
  return pin.equals(p.getProperty(user + ".pin"));
}

这很容易但是当然没有加密。该文件以纯文本形式存储,包括 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:

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 but of course there's no encryption. The file is stored in plain text including the PINs.

与风相奔跑 2024-09-22 22:54:25

来自维基百科:

数据文件是一个计算机文件,它
存储供计算机使用的数据
应用程序或系统。

因此,*.txt 文件和 *.csv 文件都是数据文件。

*.csv(逗号分隔值)文件本质上是一个 *.txt 文件,它通过 , 分隔您的值。

两种方法您发现应该做同样的事情,但使用 csv 方法会更容易。

from wikipedia:

A data file is a computer file which
stores data for use by a computer
application or system.

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.

任谁 2024-09-22 22:54:25

不管是txt还是csv都没关系。您甚至可以将其保存为 xml。
不要与文件扩展名混淆。您唯一应该关心的是“如何保存/加载这些数据”。

  1. 保存为 .txt 没问题,但也许您需要定义自己的“格式”以确保可以加载该数据。
  2. 如果将其保存为 .csv 也很好,因为您不必发明自己的“格式”。只需遵循几种旧格式,如分隔符、包围符等。
  3. 以其他格式保存也很好。例如,XML。但是,您再次需要遵循 XML 中的旧格式(例如 DTD)。

祝你好运

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".

  1. 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.
  2. 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.
  3. To save in another format is also good. For example, XML. But, again, you need to follow a legacy formatting in XML like DTD.

Good luck

平定天下 2024-09-22 22:54:25

序列化是持久存储的另一种方法。
您可以创建一个实现 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文