对象属性为相同长度的表示,以便更快地读取 [英] Object attributes to same-length representation for faster reading

查看:95
本文介绍了对象属性为相同长度的表示,以便更快地读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将我的'person'对象的属性转换为表示我可以将行写入代表每个人的文件的表示。许多机器需要同时读取它们分配的文件块,因此我将从字符编码文本行转移到字节列表。我希望每个人的表现形式是相同的长度我不必阅读每个角色,阅读应用程序可以跳到需要开始阅读的位置。

I want to convert the attributes of my 'person' object to a representation where I can write lines to a file representing each person. A number of machines need to concurrently read their allocated chunk of the file and so I am moving from lines of character encoded text to list of bytes instead. I hope that by each person's representation being the same length I don't have to read every character and the reading application can 'jump' to where it needs to start reading.

这是我到目前为止所写的内容,它采用对象的属性并将它们放入长度始终相同的表单中。我可以这样做吗?我修改了这段代码,但是我创建字符串表示并连接这些字符串。

This is what I've written so far, to take the attributes of the object and put them into a form where the length will always be the same. Can I do it like this? I modified this code from when I did the same but for creating string representations and concatenating these strings.

我也不确定我的转换布尔值的方法是否必要/ correct。

I'm also not sure if my method of converting the booleans is necessary/correct.

byte[] person = new byte[8];
person[0] = Integer.byteValue(age);
if (gender.equals('m')) {person[1] = Integer.byteValue(1);}
else {person[1] = Integer.byteValue(0);}
person[2] = Integer.byteValue(children);
person[3] = Integer.byteValue(goodHealth? 1:0);
person[4] = Integer.byteValue(cars);
person[5] = Integer.byteValue(avgWeekShopping);
person[6] = Integer.byteValue(salary);
person[7] = Integer.byteValue(smoker? 1:0);

希望能够理解我正在努力实现的目标。干杯。

Hope it can be understood what I'm trying to achieve. Cheers.

推荐答案

详细说明@ trashgod的答案,你的问题可分为两部分;编码数据并确保编码数据可以在相同长度的记录中表示。

Elaborating on @trashgod's answer, your problem can be split into two parts; encoding the data and making sure that the encoded data can be represented in records of the same length.

编码数据的明显方法如下:

The obvious way to encode the data is as follows:

byte[] bytes;
try {
    ByteArrayOutputStream baos = new ByteArrayOutputStream(/* size hint */);
    DataOutputStream dos = new DataOutputStream(baos);
    dos.writeInt(age);
    dos.writeBoolean(gender.equals('m'));
    dos.writeInt(children);
    dos.writeBoolean(goodHealth);
    dos.writeInt(cars);
    dos.writeInt(avgWeekShopping);
    dos.writeInt(salary);
    dos.writeBoolean(smoker);
    bytes = baos.toByteArray();
} catch (IOException ex) {
    throw new AssertionError("this can't happen", ex);
}

如果您有固定数量的具有基本类型的字段,那么编码的字节数组将具有固定大小。

If you have a fixed number of fields with primitive types, then the encoded byte arrays will have a fixed sized.

如果您有可变数量的字段或可变长度字符串,那么编码字节数组的大小将是可变的。要解决这个问题,您需要选择一些最大记录大小,并将所有编码的字节数组填充到该大小。

If you have variable numbers of fields, or variable length strings, then the size of the encoded byte array will be variable. To deal with this, you will need to choose some maximum record size, and pad all of the encoded byte arrays to that size.

话虽如此,如果您使用数据库而不是尝试自己进行文件管理,则可能会获得更好的整体结果。如果您的应用程序需要搜索记录,则尤其如此。

Having said that, the chances are that you'll get a better overall result if you use a database rather than trying to do file management yourself. This is especially so if your application needs to search the records.

这篇关于对象属性为相同长度的表示,以便更快地读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆