在这段代码中,我对如何返回保存结果感到困惑。我尝试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;
}
答案 0 :(得分: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创建一个新的字符串。