在 Java 的 RecordStore 中表示和存储数据的好方法是什么?

发布于 2024-08-24 19:30:41 字数 334 浏览 3 评论 0原文

鉴于 Java ME 不允许反射并且相当精简,那么在 RecordStore 中存储数据的好方法是什么?我想到设计一个类似 JSON 的语法适配器类,它可能需要每个要存储值的类来实现 Hashtable + 可能需要一个额外的正则表达式库,因为即使它已被删除。

如果你想知道我到底为什么需要它,那是为了一项任务。因此,真正的困境在于,在原则上编写相当不必要的大块正确代码,还是在知道没有人会因为维护这些垃圾而对所有内容进行硬编码之间。但是,我身上有良好原则的人倾向于前者。

编辑:我应该详细说明 - 我想将对象的数据存储在 RecordStore 中,因此我尝试设计一种方法将对象表示为字符串,然后可以将其转换为字节数组。

Given that Java ME allows for no reflection and is quite stripped down, what would a good approach to storing data in a RecordStore be? I thought of devising a JSON-like syntax adapter class which would probably require every class whose values are to be stored to implement a Hashtable + might probably require an additional regex library as even that's been taken away.

If you're wondering why on earth I need it, it's for an assignment. So the dilemma is really between writing a rather unnecessarily large chunk of proper code as a matter of principle or hardcoding everything in knowing nobody has to suffer through maintenance of this junk down the line. But, the good principles person in me is leaning towards the former.

EDIT: I should have elaborated — I'd like to store object's data within the RecordStore, so I'm trying to devise a method to represent an object as a string which can then be converted into a byte array.

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

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

发布评论

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

评论(1

耶耶耶 2024-08-31 19:30:41

对于要保存在 RecordStore 中的每个对象,将其削减为其组件字符串和基元,然后使用包装在 DataOutputStream 中的 ByteArrayOutputStream 将其序列化为字节数组。然后将此字节数组写入 RMS。

使用包装在 DataInputStream 中的 ByteArrayInputStream 反转该过程以取回对象。

For every object you want to save in the RecordStore, pare it down to its component Strings and primitives, then serialise it to a byte array, using a ByteArrayOutputStream wrapped in a DataOutputStream. Then write this byte array to RMS.

Reverse the process using a ByteArrayInputStream wrapped in a DataInputStream to get the object back.

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