Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FileOutputStream保存期间看似随机的文件损坏_Java_Io_Fileoutputstream - Fatal编程技术网

Java FileOutputStream保存期间看似随机的文件损坏

Java FileOutputStream保存期间看似随机的文件损坏,java,io,fileoutputstream,Java,Io,Fileoutputstream,我有一个Java Android应用程序,可以定期保存用户数据。它通常包含大量数据,需要几分钟才能保存。然而,它已经像这样部署了很长一段时间,并且运行良好 但是,文件会随机损坏。在打开文件时仔细调试输出后,我可以指出在读取数据时发现第一个不一致的确切位置 // This is just a small section of the data being read. I'm outputting it as it's coming in. This data is in no special po

我有一个Java Android应用程序,可以定期保存用户数据。它通常包含大量数据,需要几分钟才能保存。然而,它已经像这样部署了很长一段时间,并且运行良好

但是,文件会随机损坏。在打开文件时仔细调试输出后,我可以指出在读取数据时发现第一个不一致的确切位置

// This is just a small section of the data being read. I'm outputting it as it's coming in. This data is in no special position in the middle of the file
// (The file when last saved correctly)
// etc........
// ...........
node: 169, 1.4, 100.0, 0,   -0.0, 0.0, 0.0, 1f1f1fff
node: 222, 1.0, 100.0, 100, 45.0, 0.0, 0.0, 1f1f1fff
node: 180, 1.4, 100.0, 0,   -90.0, 0.0, 0.0, 1f1f1fff

// (The file that is corrupted)
// etc........
// ...........
node: 169, 1.4, 100.0, 0,   -0.0, 0.0, 0.0, 1f1f1fff
node: 222, 1.0, 100.0, 100, 45.0, 0.0, 0.0, 1f1f1fff
node: 180, 1.4, 100.0, 0,   0.0,  0.0, 2.3429216E-38, 7f7fffff
                            ^^^ why 0.0?, should be "-90.0", everything from here on is wrong...
FileOutputStream写入错误数据的可能原因是什么?它被包装在GZIPOutputStream中

当我将浮点值写入流时,我使用此函数:

public static void floatToOutputStream(float v, OutputStream outputStream) throws IOException {
    int bits = Float.floatToIntBits(v);
    outputStream.write((bits >> 24) & 0xff);
    outputStream.write((bits >> 16) & 0xff);
    outputStream.write((bits >> 8) & 0xff);
    outputStream.write(bits & 0xff);
}
它显然是有效的,因为所有其他字节的数据都正确地保存在这个和无数其他用户文件中。那么为什么它看起来是随机失败的呢

我注意到在Java的DataOutputStream中,它们的函数如下所示:

public static void floatToOutputStream(float v, OutputStream outputStream) throws IOException {
    intToOutputStream(Float.floatToIntBits(v), outputStream);
}

public static void intToOutputStream(int v, OutputStream outputStream) throws IOException {
    outputStream.write((v>>> 24) & 0xFF);
    outputStream.write((v>>> 16) & 0xFF);
    outputStream.write((v>>>  8) & 0xFF);
    outputStream.write((v>>>  0) & 0xFF);
}

这种变化会防止这种不一致吗?还有什么其他可能的原因?Android环境?

您可以使用BufferWriter或BufferOutputStream正确打印,而不会出现任何int-to-bits转换问题(和),谢谢,但我只是想了解为什么会出现这种特殊故障