0

在这段代码中,我对如何返回保存结果感到困惑。我尝试return saveVector;并返回baseDirectory;,但显然我无法将向量转换为字符串。我是编码的菜鸟,所以这里的任何帮助都将不胜感激,我希望你能把这个已经存在的代码撕成碎片。

public String add(String category, Question q) {
    // Get the vector of questions from a category file
    Vector<Question> Q = new Vector<Question>();
    // Add the question object to the vector
    Q.add(q);
    // Save the vector back to the category file
    ObjectOutputStream saveVector = new ObjectOutputStream(new FileOutputStream(baseDirectory));
    // Return the result of the save operation
    return saveVector;
}
4

1 回答 1

0
public String add(String category, Question q) {
    // Get the vector of questions from a category file
    Vector<Question> Q = new Vector<Question>();
    // Add the question object to the vector
    Q.add(q);
    // Save the vector back to the category file
    ByteArrayOutputStream saveVector = new ByteArrayOutputStream(new FileOutputStream(baseDirectory));
    // Return the result of the save operation
    return new String(saveVector.toByteArray());
}

改用ByteArrayOutputStream并从 ByteArrayOutputStream 的 ByteArray 创建一个新字符串。

于 2013-10-14T17:59:14.300 回答