从二进制文件中读取和打印随机元素

时间:2016-03-02 18:10:23

标签: java file binary

现在我有一个二进制文件,里面有5组问题,答案和分值。我需要从二进制文件中选择一个随机问题及其匹配的答案和点值并将其打印出来。我将如何做到这一点因为二进制文件我不认为我可以使用nextLine()等。感谢任何帮助,谢谢!

import java.io.*;

public class Trivia implements Serializable  {
private String question;
private String answer;
private int points;

public Trivia() {
    question = " ";
    answer = " ";
    points = 0;
}

public String getQuestion() {
    return question;
}

public String getAnswer() {
    return answer;
}

public int getPoints() {
    return points;
}

public void setQuestion(String q) {
    question = q;
}

public void setAnswer(String a) {
    answer = a;
}

public void setPoints(int p) {
    points = p;
}

}
import java.io.*;
import java.util.*;

public class Driver {

public static void main(String[] args) {

    Trivia[] t = new Trivia[5];
    for (int i = 0; i < 5; i++) {
        t[i] = new Trivia();
    }

    t[0].setQuestion("How many states are in the US?");
    t[0].setAnswer("50");
    t[0].setPoints(1);

    t[1].setQuestion("Who is the richest person in the US");
    t[1].setAnswer("You");
    t[1].setPoints(1);

    t[2].setQuestion("How many senators come from each state?");
    t[2].setAnswer("2");
    t[2].setPoints(2);

    t[3].setQuestion("What is the largest state?");
    t[3].setAnswer("Alaska");
    t[3].setPoints(2);

    t[4].setQuestion("Who was the thrid president?");
    t[4].setAnswer("Thomas Jefferson");
    t[4].setPoints(3);

    ObjectOutputStream outputStream = null;

    try {
        outputStream = new ObjectOutputStream(new FileOutputStream("trivia.dat"));

    } catch (IOException e) {
        System.out.println("Could not open file");
        System.exit(0);
    }

    try {
        outputStream.writeObject(t);
        outputStream.close();
    } catch (IOException e) {
        System.out.println("Writing error");
        System.exit(0);
    }

    ObjectInputStream inputStream = null;


    try {
        inputStream = new ObjectInputStream(new FileInputStream("trivia.dat"));

    } catch (IOException e) {
        System.out.println("File not found.");
        System.exit(0);
    }
    Trivia[] test = null;

    try {
        test = (Trivia[]) inputStream.readObject();
    } catch (Exception e) {
        System.out.println("Reading error");
        System.exit(0);
    }

}

ArrayList<Trivia> triviaQuestions = new ArrayList<Trivia>(5);
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("trivia.dat"));
triviaQuestions.add((Trivia) ois.readObject()); // This should be repeated (looped) until all questions are read


}

以下是二进制文件的屏幕截图: http://imgur.com/a/zask2

1 个答案:

答案 0 :(得分:0)

假设问题的数量不是那么大,我建议在将问题对象(包括theire answer属性和点)存储在列表中时读取完整文件。然后,您可以从此列表中请求随机问题。

List<Question> questions = readQuestions();
Question randomQuestion = questions.get((new Random()).nextInt(questions.size()));

有关二进制文件的更新

看起来该文件是使用ObjectOutputStream创建的。要阅读它,可以使用ObjectInputStream类。逐个读取每个对象将其添加到列表中。您可以像这样使用它:

ObjectInputStream in = new ObjectInputStream(new FileInputStream("path_to_file"));
tiviaQuestions.add((Trivia) in.readObject()); // This should be repeated (looped) until all questions are read

您的最终驱动程序类应如下所示:

public class Driver {

public static void main(String[] args) {

    Trivia[] t = new Trivia[5];
    for (int i = 0; i < 5; i++) {
        t[i] = new Trivia();
    }

    t[0].setQuestion("How many states are in the US?");
    t[0].setAnswer("50");
    t[0].setPoints(1);

    t[1].setQuestion("Who is the richest person in the US");
    t[1].setAnswer("You");
    t[1].setPoints(1);

    t[2].setQuestion("How many senators come from each state?");
    t[2].setAnswer("2");
    t[2].setPoints(2);

    t[3].setQuestion("What is the largest state?");
    t[3].setAnswer("Alaska");
    t[3].setPoints(2);

    t[4].setQuestion("Who was the thrid president?");
    t[4].setAnswer("Thomas Jefferson");
    t[4].setPoints(3);

    ObjectOutputStream outputStream = null;

    try {
        outputStream = new ObjectOutputStream(new FileOutputStream("trivia.dat"));

    } catch (IOException e) {
        System.out.println("Could not open file");
        System.exit(0);
    }

    try {
        outputStream.writeObject(t);
        outputStream.close();
    } catch (IOException e) {
        System.out.println("Writing error");
        System.exit(0);
    }

    ObjectInputStream inputStream = null;
    ArrayList<Trivia> triviaQuestions = new ArrayList<Trivia>();

    try {
        inputStream = new ObjectInputStream(new FileInputStream("trivia.dat"));

        for(int i=0; i<5; i++){ // Repeats the content of the loop five times
            triviaQuestions.add((Trivia) inputStream.readObject());
        }
        inputStream.close(); // Closes the input stream because it is not longer needed


    } catch (IOException e) {
        System.out.println("File not found.");
        System.exit(0);
    }

    Trivia yourRandomTrivia = triviaQuestions.get((new Random()).nextInt(triviaQuestions.size())); // This will be your random question

}

// You did not get an auto complete suggestion because you typed outside of a method


}