- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的输入数据集是二进制格式的 16 位整数(2 字节)的 1201x1201 个元素。文件总大小为 2884802 字节。我使用 ByteBuffer 将这些数据读入 Java,然后使用 ObjectOutputStream 的 writeShort() 方法将其写为无符号短整型的二维数组。现在我的文件大小是 2898893 字节。为什么会出现这种差异?
FileChannel fileInputChannel = new FileInputStream(fileInput).getChannel();
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileOutput));
short[][] data = new short[1201][1201];
ByteBuffer bb = ByteBuffer.allocateDirect(2884802);
while (bb.remaining() > 0)
fileInputChannel.read(bb);
fileInputChannel.close();
bb.flip();
ShortBuffer sb=null;
if (ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN))
{
sb = bb.order(ByteOrder.BIG_ENDIAN).asShortBuffer();
}
else
{
sb = bb.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer();
}
for (int i=0;i<1201;i++)
{
for (int j=0;j<1201;j++)
{
data[i][j] = sb.get();
oos.writeShort(data[i][j] & 0xFFFF);
}
}
最佳答案
不要使用DataOutputStream
,这意味着要在序列化中使用。使用您已有的FileOutputStream
,如下所示:
FileChannel fileInputChannel = new FileInputStream(fileInput).getChannel();
FileOutputStream fos = new FileOutputStream(fileOutput);
short[][] data = new short[1201][1201];
ByteBuffer bb = ByteBuffer.allocateDirect(2884802);
while (bb.remaining() > 0)
fileInputChannel.read(bb);
fileInputChannel.close();
bb.flip();
ShortBuffer sb=null;
if (ByteOrder.nativeOrder().equals(ByteOrder.BIG_ENDIAN))
{
for (int i=0;i<1201;i++)
{
for (int j=0;j<1201;j++)
{
fos.write((data[i][j] >> 8) & 0xFF);
fos.write(data[i][j] & 0xFF);
}
}
}
else
{
for (int i=0;i<1201;i++)
{
for (int j=0;j<1201;j++)
{
fos.write(data[i][j] & 0xFF);
fos.write((data[i][j] >> 8) & 0xFF);
}
}
}
关于Java输入输出二进制文件字节大小转换后不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26098541/
我收到了一个充满详细信息的文件,必须将其提取并根据其各自的列存储到不同的对象数组中。棘手的是,并非所有线路都具有相同数量的数据。我可以读取文件,可以拆分它以及所有内容。 20005,Arsenal,M
本题与问题相关:如何检测intent(in)的违规子程序里面。但是我在相关问题Enforce intent(in) declared variables in Fortran as constant
我正在尝试用 Java 设计一个海里计算器。 我能够通过控制台创建和运行它,但我在学习如何在 JFrames 中执行它时遇到了麻烦。 我基本上想做的是: 有两个文本字段和一个按钮,一个文本字段英里,一
我有一个 Excel 文件,其中显示了一个表格,用户可以在其中更改字段的值。当此值更改时,另一个字段的值会自动更改,因为它具有以下公式: row 26 =IF(DWTDwt:
我已经成功地从 2 个文件中提取数据并将它们存储在各自类的数组对象中。这是我存储在数组对象中的一些数据: 2005,Arsenal,ManU,Arsenal,WestHam,Y,2,3,40000 2
谁能告诉我这段代码有什么问题?我总是打不开。 #include #include using namespace std; int main(){ fstream fs; fs.o
golang 的fmt 包实现了格式化I/O函数,类似于C的 printf 和 scanf 定义示例类型和变量 ?
我正在编写一个脚本,它将一个 UTF-16 编码的文本文件作为输入并输出一个 UTF-16 编码的文本文件。 use open "encoding(UTF-16)"; open INPUT, " in
我刚刚开始学习 Java,我想克服在尝试为这个“问题”创建 Java 程序时出现的障碍。这是我必须创建一个程序来解决的问题: Tandy 喜欢分发糖果,但只有 n 颗糖果。对于她给第 i 个糖果的人,
代码在一台服务器上运行,但在另一台服务器上运行不正常。尝试使用 jquery uploadify 上传大小约为 1 MB 的文件时出现此错误。 IO: Error #2038 尝试在 php.ini
认识文件 平时说的文件一般都是指存储在硬盘上的普通文件。 形如:txt文本、jpg图片、mp4视频、rar压缩包等这些文件都可以认为是普通文件。 它们都是在硬盘上存储的。 但是站在计算机专业术语的角度
是否可以同时使用 C++ 应用程序和 Java 应用程序读取和写入同一个文本文件,而不会向其中写入冲突的行/字符?我现在已经用两个 java 应用程序进行了测试,似乎可以从一个进程写入文件,即使另一个
我是一名优秀的程序员,十分优秀!