为txt中的对象导入多行数据。文件
Import multiple lines of data for object in txt. file
我目前正在尝试创建一个小测验应用程序,其中对象 class 将包含:
- 问题
- answerChoice1
- answerChoice2
- answerChoice3
- 正确答案数
为了包含这些数据,我有一个 txt。包含相关数据的文件。该文件的格式也与上面的列表相同。
我想做的是读取文件并从每一行导入数据并转换为我的程序使用。
这与这种性质的其他问题不同,因为我希望转换问题(其中字符串中会有空格),同时还在不同的行中为数组中的同一对象集导入其他对象数据。
questions (String question, String answer1, String answer2, String answer3, int correctAnswer){
this.question = question;
this.answer1 = answer1;
this.answer2 = answer2;
this.answer3 = answer3;
this.correctAnswer = correctAnswer;
}
public String getQuestion(){
return question;
}
public String getAnswer1(){
return answer1;
}
public String getAnswer2(){
return answer2;
}
public String getAnswer3(){
return answer3;
}
public int getCorrectAnswer(){
return correctAnswer;
}
这是我目前的对象class,如果需要的话。
您可以在 class 中实现 Serializable 接口。这是一个接口标记,因此它不包含任何方法和字段。 类 不实现此接口的状态将不会被序列化或反序列化。
您必须必须使用这些确切的签名来实现特殊方法:
private void writeObject(java.io.ObjectOutputStream out) throws IOException
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;
因此,您的 "writeObject()" 方法将如下所示:
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.writeString(question);
out.writeString(answer1);
out.writeString(answer2);
out.writeString(answer3);
out.writeInt(correctAnswer);
}
而你的 "readObject" 将是这样的:
private void writeObject(java.io.ObjectInputStream in) throws IOException,ClassNotFoundException {
question = in.readString();
answer1 = in.readString();
answer2 = in.readString();
answer3 = in.readString();
correctAnswer = in.readInt();
}
我目前正在尝试创建一个小测验应用程序,其中对象 class 将包含:
- 问题
- answerChoice1
- answerChoice2
- answerChoice3
- 正确答案数
为了包含这些数据,我有一个 txt。包含相关数据的文件。该文件的格式也与上面的列表相同。
我想做的是读取文件并从每一行导入数据并转换为我的程序使用。
这与这种性质的其他问题不同,因为我希望转换问题(其中字符串中会有空格),同时还在不同的行中为数组中的同一对象集导入其他对象数据。
questions (String question, String answer1, String answer2, String answer3, int correctAnswer){
this.question = question;
this.answer1 = answer1;
this.answer2 = answer2;
this.answer3 = answer3;
this.correctAnswer = correctAnswer;
}
public String getQuestion(){
return question;
}
public String getAnswer1(){
return answer1;
}
public String getAnswer2(){
return answer2;
}
public String getAnswer3(){
return answer3;
}
public int getCorrectAnswer(){
return correctAnswer;
}
这是我目前的对象class,如果需要的话。
您可以在 class 中实现 Serializable 接口。这是一个接口标记,因此它不包含任何方法和字段。 类 不实现此接口的状态将不会被序列化或反序列化。 您必须必须使用这些确切的签名来实现特殊方法:
private void writeObject(java.io.ObjectOutputStream out) throws IOException
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;
因此,您的 "writeObject()" 方法将如下所示:
private void writeObject(java.io.ObjectOutputStream out) throws IOException {
out.writeString(question);
out.writeString(answer1);
out.writeString(answer2);
out.writeString(answer3);
out.writeInt(correctAnswer);
}
而你的 "readObject" 将是这样的:
private void writeObject(java.io.ObjectInputStream in) throws IOException,ClassNotFoundException {
question = in.readString();
answer1 = in.readString();
answer2 = in.readString();
answer3 = in.readString();
correctAnswer = in.readInt();
}