【发布时间】:2009-10-02 20:09:36
【问题描述】:
我有一个需要转换为字节数组的 bin 文件。谁能告诉我该怎么做?
这是我目前所拥有的:
File f = new File("notification.bin");
is = new FileInputStream(f);
long length = f.length();
/*if (length > Integer.MAX_VALUE) {
// File is too large
}*/
// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];
// Read in the bytes
int offset = 0;
int numRead = 0;
while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
// Ensure all the bytes have been read in
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+f.getName());
}
但它不起作用......
卡迪
【问题讨论】:
-
我为你解决了这个问题。请尝试实际格式化您发布的问题,以便它们智能且清晰。
-
在什么情况下它不起作用?您使用的 File 类是什么?为什么不使用标准的 std::ifstream?
-
可能是因为它是 C# 而不是 C++
-
那不是 C# - 我相信是 Java。
-
无论如何,它看起来确实更像 Java 而不是 C#。我已经相应地编辑了标签。