用java将数据写入文件

发布于 2024-10-20 00:18:50 字数 296 浏览 5 评论 0原文

我正在用 java 编写一个服务器,允许客户端玩类似于 20 个问题的游戏。游戏本身基本上是一个二叉树,其节点是关于对象的问题,叶子是对对象身份的猜测。当游戏猜测错误时,它需要能够从玩家那里得到正确的答案并将其添加到树中。然后该数据被保存到随机访问文件中。

问题是:如何在文件中表示树,以便稍后可以将数据作为树重新访问。

如果您知道在哪里可以找到有关在写入/读取文件时保持树等数据结构组织良好的信息,请链接它。多谢。


感谢大家的快速解答。这是一个学校项目,因此它有一些奇怪的要求,例如使用随机访问文件和 telnet。

I am writing a server in java that allows clients to play a game similar to 20 questions. The game itself is basically a binary tree with nodes that are questions about an object and leaves that are guesses at the object's identity. When the game guesses wrong it needs to be able to get the right answer from the player and add it to the tree. This data is then saved to a random access file.

The question is: How do you go about representing a tree within a file so that the data can be reaccessed as a tree at a later time.

If you know where I can find information on keeping data structures like trees organized as such when writing/reading to files then please link it. Thanks a lot.


Thanks for the quick answers everyone. This is a school project so it has some odd requirements like using random access files and telnet.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

冰葑 2024-10-27 00:18:50

然后,该数据将保存到随机访问文件中。

这是解决您的问题的困难方法(我的意思是“随机访问”位)。

你真正想要解决的问题是如何持久化一个“复杂”的数据结构。事实上,有多种方法可以做到这一点。以下是其中的一些...

  • 使用 Java 持久性。这很容易实现;确保您的数据结构是可序列化的,然后只需几行代码即可序列化,再添加几行代码即可反序列化。缺点是:

    • 序列化对象在代码更改时可能很脆弱。
    • 序列化不是增量的。您每次都会写入/读取整个图表。
    • 如果您有多个单独的序列化图表,则需要某种方案来命名和管理它们。
  • 使用 XML。与 Java 持久性相比,这需要更多的工作来实现,但它的优点是不那么脆弱。如果出现问题,您可以使用 XSLT 或文本编辑器来修复它。 (有一些 XML“绑定”库可以消除大量粘合编码。)

  • 使用 SQL 数据库。这解决了 Java 持久性的所有缺点,但涉及更多编码......并使用不同的计算模型来访问持久性数据(查询与图形导航)。

  • 使用数据库和对象关系映射技术;例如 JPA 或 JDO 实现。 (Hibernate 是一个流行的选择)。这些以或多或少透明的方式在数据库和内存数据视图之间建立了桥梁,并避免了在 SQL 数据库和 XML 情况下需要编写的大量粘合代码。

This data is then saved to a random access file.

That's the hard way to solve your problem (the "random access" bit, I mean).

The problem you are really trying to solve is how to persist a "complicated" data structure. In fact, there are a number of ways that this can be done. Here are some of them ...

  • Use Java persistence. This is simple to implement; make sure that your data structure is serializable, and then its just a few lines of code to serialize and few more lines to deserialize. The downsides are:

    • Serialized objects can be fragile in the face of code changes.
    • Serialization is not incremental. You write/read the whole graph each time.
    • If you have multiple separate serialized graphs, you need some scheme to name and manage them.
  • Use XML. This is more work to implement than Java persistence, but it has the advantage of being less fragile. And if something does go wrong, there's a chance you can fix it with XSLT or a text editor. (There are XML "binding" libraries that eliminate a lot of the glue coding.)

  • Use an SQL database. This addresses all of the downsides of Java persistence, but involves more coding ... and using a different computational model to access the persistent data (query versus graph navigation).

  • Use a database and an Object Relational Mapping technology; e.g. a JPA or JDO implementation. (Hibernate is a popular choice). These bridge between the database and in-memory views of data in a more or less transparent fashion, and avoids a lot of the glue code that you need to write in the SQL database and XML cases.

做个少女永远怀春 2024-10-27 00:18:50

如前所述,您正在寻找序列化。它允许您将对象写入文件,然后以最小的努力将其读回。该文件将自动作为您的对象类型读回。这使得事情比尝试使用 XML 自己存储对象要容易得多。

As mentioned, serialization is what you are looking for. It allows you to write an object to a file, and read it back later with minimal effort. The file will automatically be read back in as your object type. This makes things much easier than trying to store the object yourself using XML.

雾里花 2024-10-27 00:18:50

Java 序列化有一些陷阱(例如当您更新类时)。我会以文本格式序列化。 Json 是我的第一选择,但 xml 和 yaml 也可以。

这样,您将拥有一个不依赖于类的二进制版本的文件。

有几个 java 库: http://www.json.org

一些示例:

http://code.google.com/p/json-simple/wiki/DecodingExamples

http://code.google.com/p/json-simple/wiki/EncodingExamples

要保存和读取文件,您可以使用 Commons Io

import org.apache.commons.io.FileUtis;
import java.io.File;
...
File dataFile = new File("yourfile.json");
String data = FileUtils.readFileToString(dataFile);

FileUtils.writeStringToFile(dataFile, content);

Java serialization has some pitfalls (like when you update your class). I would serialize in a text format. Json is my first choice here but xml and yaml would work as well.

This way you would have a file that doesn't rely on the binary version of your class.

There are several java libraries: http://www.json.org

Some examples:

http://code.google.com/p/json-simple/wiki/DecodingExamples

http://code.google.com/p/json-simple/wiki/EncodingExamples

And to save and read from the file you can use the Commons Io:

import org.apache.commons.io.FileUtis;
import java.io.File;
...
File dataFile = new File("yourfile.json");
String data = FileUtils.readFileToString(dataFile);

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