仅使用File / ObjectInputStreams从代码中消除读者

时间:2013-10-14 03:41:28

标签: java filereader fileinputstream objectinputstream

我清楚地说明了我在标题中寻找的内容。我提供了我想要解决的代码,我需要的所有方向。我现在唯一关心的是不惜一切代价取消读者作为这项任务的规定。

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

2 个答案:

答案 0 :(得分:0)

您的代码甚至无法编译,这使您的问题已经没有实际意义,并且您无法将String投射到Question.

但是,如果你有义务按照这些相同的规定阅读这些内容,你将不得不转向DataInputStream.readLine(),这个被弃用的,这也可能违反你的规定,这会使他们自相矛盾。其他所有内容都可以使用流API完成。

答案 1 :(得分:-1)

如果要删除代码中使用的所有读者并将其替换为File / ObjectInputStream,那么我认为不可能。 无论如何,你需要一个读者来阅读流。据我所知,没有读者或写作者,你无法操纵流