Can someone please show me how to get this code working correctly. It doesn\'t h
ID: 3543857 • Letter: C
Question
Can someone please show me how to get this code working correctly. It doesn't have to be exact and I know you don't know the exact methodNames or variables, just show how to do the steps listed in this code with accurate java coding.
private String saveFile(String category, Vector<Question> questions) {
// Create the file object:
// category is a file name inside the base directory
// Inside a try block,
// Create FileOutputStream for the category file
// Create ObjectOutputStream for the category file
// Write the vector to the file
// Flush the object stream, the close both streams
// Return a string indicating success
// Catch blocks:
// Return an error string indicating the problem
}
Explanation / Answer
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, then close both streams
save.flush();
save.close();
saveFile.close();
// Return a string indicating success
return "Success";
}
// Catch blocks:
catch(Exception E){
E.printStackTrace();
// Return an error string indicating the problem
return "Error in Writing to the File " + category;
}
}