【问题标题】:Return the result of the save返回保存结果
【发布时间】:2013-10-22 08:50:49
【问题描述】:

在这段代码中,我对如何返回保存结果感到困惑。我尝试了 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;
}

【问题讨论】:

  • 我认为您需要发布更多代码,以及您想要实现的目标。您当前发布的代码创建了一个新的 Vector,添加了一个元素,并在您继续创建 ObjectOutputStream 时让它腐烂,我怀疑它甚至应该在 add() 方法中。
  • 好的。这是一个包含三个独立类的项目。我知道这个块是错误的,需要最多的工作,但我对如何让它正常工作感到迷茫。如果有帮助,我可以链接所有三个类。
  • 创建者:ideone.com/N1AUR1> 问题:ideone.com/hhyHsk> QuestionManager:ideone.com/sTUVkK>
  • 这是一个好的开始。现在,您到底想达到什么目的? add() 应该做什么?

标签: java string vector return


【解决方案1】:
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 创建一个新字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-27
    • 2021-03-22
    • 2015-06-03
    • 2013-06-21
    相关资源
    最近更新 更多