我非常清楚地说明了我在标题中寻找的内容。我已经提供了我想要修复的代码以及我需要的所有方向。此刻我唯一关心的是不惜一切代价消除读者,这是这项任务的一项规定。
import java.io.File;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Vector;
import java.util.Iterator;
public class QuestionManager {
String baseDirectory;
public QuestionManager(String baseDirectory) {
// Store the name of the base directory
// and create that directory if it does not exist
this.baseDirectory = baseDirectory;
File dir = new File(baseDirectory);
if (!dir.exists())
dir.mkdir();
}
public String add(String category, Question q) {
// Get the vector of questions from a category file
File f = new File(category);
f.createNewFile();
Vector<Question> Q = new Vector<Question>();
BufferedReader reader = new BufferedReader(new FileReader(f));
String line = null;
while((line = reader.readLine())!= null) {
Q.add(Question)line);
}
// Add the question object to the vector
Q.add(q);
// Save the vector back to the category file
Iterator i = new Iterator<Question>(Q);
FileWriter w = new FileWriter(f);
while(i.hasNext()) {
w.write((int)(i.next()));
}
w.flush();
w.close();
// Return the result of the save operation
return Q;
}
public Question get(String category, int num) {
// Get the vector of questions from a category file
Vector<Question> questions = getCategoryFile(category);
// Check that the index number is valid (between 0 and the vector size - 1)
if (num >= 0 && num < questions.size()) {
// If valid, return the question object
return questions.elementAt(num);
}
else {
// else return null
return null;
}
}
public String update(String category, Question q, int num) {
// Get the vector of questions from a category file
Vector<Question> questions = getCategoryFile(category);
// String variable to hold the result of save operation
String result;
// Check that the index number is valid (between 0 and the vector size - 1)
if (num >= 0 && num < questions.size()) {
// If valid, replace the question at that index in the vector with the new
questions.set(num, q);
// question object, then save the file and return the result of the
// save operation
result = saveFile (category, questions);
return result;
}
else {
// If index is invalid return a string indicating that.
return "Invalid Index!";
}
}
private String saveFile(String category, Vector<Question> questions) {
// Create the file object:
// category is a file name inside the base directory
File F = new File(category);
// Inside a try block,
try {
// Create FileOutputStream for the category file
FileOutputStream saveFile = new FileOutputStream(F, true);
// Create ObjectOutputStream for the category file
ObjectOutputStream save = new ObjectOutputStream(saveFile);
// Write the vector to the file
save.writeObject(questions);
// Flush the object stream, the close both streams
save.flush();
save.close();
saveFile.close();
// Return a string indicating success
return "Success";
}
// Catch blocks:
catch (Exception E) {
// Return an error string indicating the problem
return "Error in writing to the file " + category;
}
}
private Vector<Question> getCategoryFile(String category) {
//Create the file object:
// category is a file name inside the base directory
File F = new File(category);
// Create an empty vector of questions
Vector<Question> questionVector;
// Inside a try block,
try {
// Create FileInputStream for the category file
FileInputStream saveFile = new FileInputStream(F);
// Create ObjectInputStream for the category file
ObjectInputStream save = new ObjectInputStream(saveFile);
// Read the vector from the file
questionVector = (Vector<Question>) save.readObject();
// Close both streams
save.close();
saveFile.close();
}
// Catch blocks:
catch (Exception E) {
E.printStackTrace();
// Create an empty vector of questions
questionVector = new Vector<Question>();
}
// Return the vector
return questionVector;
}
}