헤더에서 매우 명확하게 찾고있는 내용을 언급했습니다. 내가 필요한 모든 지침으로 수정하려고하는 코드를 제공했습니다. 이 순간에 내가 염려하는 유일한 것은 독자가이 과제에서 규정 한대로 모든 비용을 감축하는 것입니다. 당신이 코드에 사용 된 모든 독자를 제거하고 파일로 교체 할 경우File/ObjectInputStreams 만 사용하여 코드에서 독자 제거
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;
}
}
여기서'f.createNewFile()'을 호출 할 필요가 없습니다. 어쨌든'new FileWriter()'는 그렇게합니다. 'close()'의 직전에'flush()'를 호출 할 필요가 없습니다. – EJP