0

我非常清楚地说明了我在标题中寻找的内容。我已经提供了我想要修复的代码以及我需要的所有方向。此刻我唯一关心的是不惜一切代价消除读者,这是这项任务的一项规定。

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;
    }
}
4

2 回答 2

0

您的代码甚至没有编译,这使得您的问题已经没有实际意义,而且您永远无法将 aString转换为Question.

但是,如果您必须按照这些相同的规定阅读行,您将不得不转移到DataInputStream.readLine(),不推荐使用的行,这也可能违反您的规定,这会使它们自相矛盾。其他一切都可以使用流 API 完成。

于 2013-10-14T04:02:15.513 回答
-1

如果您想删除您在代码中使用的所有阅读器并用 File/ObjectInputStream 替换它们,那么我认为这是不可能的。无论如何,您都需要一个阅读器来阅读流。据我所知,没有读者或作家你无法操纵流

于 2013-10-14T03:47:00.407 回答