【发布时间】:2012-07-11 08:09:03
【问题描述】:
我正在使用以下方式读取文件:
int len = (int)(new File(args[0]).length());
FileInputStream fis =
new FileInputStream(args[0]);
byte buf[] = new byte[len];
fis.read(buf);
我发现here。是否可以将 byte array buf 转换为 Int Array ?将Byte Array 转换为Int Array 会占用更多空间吗?
编辑:我的文件包含数百万个整数,例如,
100000000 200000000 .....(使用普通 int 文件写入)。我把它读到字节缓冲区。现在我想将它包装到 IntBuffer 数组中。怎么做 ?我不想将每个字节转换为 int。
【问题讨论】:
-
为什么要将字节数组转换为int数组?
-
@vidit,因此我可以读取整数,如 int c = array[0]
-
只是循环并将值复制到一个新的 int 数组中?
-
“将占用更多空间”每个
int存储在 32 位上,byte存储在 8 位上,因此将占用大约 4 倍的空间 -
@alessandro 是否要将数组转换为将每个
4 bytes 存储在一个int元素中?在这种情况下,this 你可能会感兴趣。
标签: java arrays int byte endianness