【发布时间】:2014-07-19 03:42:36
【问题描述】:
我正在尝试获取二进制数据,“反序列化?”它从文件statePopulationPercentages.dat 中转换为可用数据。
然后我想将整数添加到名为percentages 的ArrayList<Integer>,但对于初学者,只需将它们打印出来。
我真的是 java 新手,并且使用二进制文件,所以任何帮助都会很棒。这是我到目前为止所得到的。
try {
// Open the file for reading.
RandomAccessFile raf = new RandomAccessFile("statePopulationPercentages.dat", "r");
long position = raf.length();
while (position > 0) {
position -= 1;
raf.seek(position);
byte b = raf.readByte();
// line = deserialize "b"
// add to ArrayList<Integer> percentages
// percentages.add(line);
System.out.print((int) b); // this would obviously go away when adding to the array list
}
} catch (Exception e) {
e.printStackTrace();
}
【问题讨论】:
-
你需要先指定文件格式。
标签: java deserialization binaryfiles