0
我知道這已被問了很多次,但我已經嘗試了所有人的建議代碼,並且無法在我的系統上運行它。無法在Java中使用DataOutputStream編寫整數
public class Heap implements Serializable {
public Attribute[] A; // Array of data, has elements name/String, p1/int, n1/int, ig/double
private int n = 0; // num elements
private int max = 0; // Max elements
...
}
public void serialize(Heap hp, String filename) throws IOException {
DataOutputStream dst = new DataOutputStream(new FileOutputStream(filename));
dst.writeInt(hp.heapMax());
dst.writeInt(hp.A.length);
System.out.println("Writing heap max " + hp.heapMax() + " and heap size " + hp.A.length);
for(int i = hp.A.length - 1; i > 0; i--) {
dst.writeInt(hp.A[i].n1);
dst.writeInt(hp.A[i].p1);
dst.writeDouble(hp.A[i].ig);
dst.writeUTF(hp.A[i].name);
}
dst.close();
}
該Heap對象在Attributes數組中有1000個項目。但是,當我寫入數據時,當我在vi中打開文件時,在文件中看到的全部數據爲:^@^@^Cè^@^@^Cé
。十六進制編輯器只顯示我00 00 03 E8 00 00 03 E9
。那麼我做錯了什麼?
的java -version
輸出:
java version "1.7.0_25"
Java(TM) SE Runtime Environment (build 1.7.0_25-b15)
Java HotSpot(TM) 64-Bit Server VM (build 23.25-b01, mixed mode)
它只是'writeInt'方法給你帶來麻煩嗎? –