Java 如何获取二进制文件,读取它,将其转换为可用数据,并将其存储到ArrayList中
我试图从文件Java 如何获取二进制文件,读取它,将其转换为可用数据,并将其存储到ArrayList中,java,deserialization,binaryfiles,Java,Deserialization,Binaryfiles,我试图从文件stateplopulationpercentages.dat中将二进制数据“反序列化”为可用数据 然后我想将整数添加到名为百分比的ArrayList,但对于初学者,只需将它们打印出来即可 我对java和二进制文件都是新手,所以任何帮助都会很好。这是我到目前为止得到的 try { // Open the file for reading. RandomAccessFile raf = new RandomAccessFile("statePop
stateplopulationpercentages.dat
中将二进制数据“反序列化”为可用数据
然后我想将整数添加到名为百分比的ArrayList
,但对于初学者,只需将它们打印出来即可
我对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();
}
试试看{
//打开文件进行读取。
RandomAccessFile raf=新的RandomAccessFile(“statePopulationPercentages.dat”,“r”);
长位置=raf.长度();
同时(位置>0){
位置-=1;
寻找(位置);
字节b=raf.readByte();
//行=反序列化“b”
//添加到ArrayList百分比
//百分比。添加(行);
System.out.print((int)b);//添加到数组列表时,这显然会消失
}
}捕获(例外e){
e、 printStackTrace();
}
假设文件是通过序列化Java对象创建的,这是一个页面,其中包含有关将对象反序列化为数组列表的信息:
您需要从指定文件格式开始。