Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can someone please show me how to get this code working correctly. It doesn\'t h

ID: 3543858 • 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 Vector<Question> getCategoryFile(String category) {
        //Create the file object:
            // category is a file name inside the base directory
        // Inside a try block,
            // Create FileInputStream for the category file
            // Create ObjectInputStream for the category file
            // Read the vector from the file
            // Close both streams
        // Catch blocks:
            // Create an empty vector of questions
        // Return the vector
    }

Explanation / Answer

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;

}