【问题标题】:I wanted to make an array out of the .dat file我想从 .dat 文件中创建一个数组
【发布时间】:2021-07-06 05:16:29
【问题描述】:

我有一个 .dat 文件,其中包含使用对象类型编写的问题和答案

ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("QuestionBank.dat"));

我试图将它们放入一个用“-”分隔的数组中,但它对我不起作用。 谁能帮帮我?

这是我的代码:

public static void main(String[] args) throws IOException, ClassNotFoundException {
    Object line = "";
    String[] qqq = new String[100000];
    try (ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("QuestionBank.dat"));) {
        output.writeObject("Q: Q1 ...");
        output.writeObject("A ...");
        output.writeObject("B ...");
        output.writeObject("C ...");
        output.writeObject("D ...");
        output.writeObject("-");
        output.writeObject("Q: Q2 ...");
        output.writeObject("A ...");
        output.writeObject("B ...");
        output.writeObject("C ...");
        output.writeObject("D ...");
        output.writeObject("-");
        output.writeObject("Q: Q3 ...");
        output.writeObject("A ...");
        output.writeObject("B ...");
        output.writeObject("C ...");
        output.writeObject("D ...");

    }
    try (ObjectInputStream input = new ObjectInputStream(new FileInputStream("QuestionBank.dat"));) {

        while ((line = input.readObject()) != null) {
            qqq = ((String) line).split("-");
            System.out.println(Arrays.toString(qqq));

        }

    }

}

}

【问题讨论】:

  • 预期结果是什么?

标签: java arrays file


【解决方案1】:

您可以读取文件内容并按如下方式拆分

List<String> lines = Files.readAllLines(Paths.get(PathOfQuestionBankFile), StandardCharsets.US_ASCII);
String.join("", lines);
qqq = lines.split("-");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多