【问题标题】:FileOutputStream write non-existent bytesFileOutputStream 写入不存在的字节
【发布时间】:2017-04-20 17:19:13
【问题描述】:

我的 FileOutputStream 有问题。

我使用这个简单的代码:

byte[] data1 = BD25Writer.getData1();
byte[] data2 = BD25Writer.getData2();

FileOutputStream dbf = new FileOutputStream(new File(file.getPath(), "file1.dbf"));
dbf.write(data1);
dbf.close();

FileOutputStream dbt = new FileOutputStream(new File(file.getPath(), "file2.dbt"));
dbt.write(data2);
dbt.close();

文件可在大多数平台(Windows、Android)上正确写入。在 HEX 编辑器中,它看起来像这样:

但仅在一台设备(YotaPhone2,型号:YD201,android 6.0.1)上,此具有相同字节的代码无法正常工作。写入文件的不存在字节:

方法 .getData1() 和 .getData2() 可以正常工作。这些方法只是将浮点数据写入字节数组。 Сode 看起来像这样:

private static byte[] getData1(){
    int key = ...;
    float[] buffer = new float[]{...};
    byte[] array = new byte[buffer.length * 4];
    byte[] bytes = new byte[4];

    for (int i = 0; i < buffer.length; i++) {
        bytes = ByteBuffer.allocate(4).putFloat(buffer[i]).array();
        array[key + i * 4] = bytes[3];
        array[key + i * 4 + 1] = bytes[2];
        array[key + i * 4 + 2] = bytes[1];
        array[key + i * 4 + 3] = bytes[0];
    }
    return array;
} 

在将数组发送到 FileOutputStream 之前,数组如下所示(从调试中获取):

这是怎么回事,我该如何解决?

【问题讨论】:

  • 0D 0A 不是 CR/LF 的代码吗?您确定getData() 中的byte[] 没有这些条目吗?
  • 我的猜测是 FileOutputStream 工作正常,并且那些 0D 字节在数据中。你应该edit你的问题包括这个BD25Writer类的相关部分,并显示数据最终来自哪里。
  • 感谢您的回复。我添加了更多信息。我确定 getData() 中的 byte[] 是正确的。检查调试器。

标签: java fileoutputstream


【解决方案1】:

事实证明,所有代码都能正常工作。邮件客户端在发送过程中破坏了文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-09
    • 2019-09-06
    • 2015-02-21
    • 2017-11-10
    • 1970-01-01
    • 2014-01-17
    • 2016-11-23
    • 2015-03-07
    相关资源
    最近更新 更多