【问题标题】:How to take a binary file, read it, convert it into usable data, and store it into an ArrayList如何获取二进制文件,读取它,将其转换为可用数据,并将其存储到 ArrayList
【发布时间】:2014-07-19 03:42:36
【问题描述】:

我正在尝试获取二进制数据,“反序列化?”它从文件statePopulationPercentages.dat 中转换为可用数据。

然后我想将整数添加到名为percentagesArrayList<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


【解决方案1】:

假设文件是​​通过序列化 Java 对象创建的,这是一个包含将对象反序列化为 ArrayList 的信息的页面:

Deserialize multiple Java Objects

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    相关资源
    最近更新 更多