Hello,
Working on my final project for a Java Class and have become stumped on the final leg of the code.
We are supposed to create a program to add a record, remove a record, display a record, and list all records. The records must be written to a file.
I have all of it working except the file part. I am able to get the program to write my array, (Which is what I am using to store and manipulate the records during program running), but I am having trouble getting it to read in the contents Back into the array.
The relevant code is:
/*--------------------SAVE DATABASE-----------------------*/
public static void SaveDatabase(String plateDB[][])
{
String tempFileName = "";
Scanner inputDevice = new Scanner(System.in);
System.out.print("Please enter a filename to save database as:");
tempFileName = inputDevice.nextLine();
File database = new File(tempFileName);
try
{
FileOutputStream fos = new FileOutputStream(database);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(plateDB); // Write the String array to a file
oos.close();
}
catch(IOException e)
{
System.err.println("Error opening file!");
}
}
This is what I am using to write the array out to the file, It asks for a file name and just saves to the file, (I intend to add some error handling later, like if the file already exists).
/*--------------------LOAD DATABASE-----------------------*/
public static String[][] LoadDatabase()
{
String[][] plateDB = new String[100][3];
String tempFileName = "";
Scanner inputDevice = new Scanner(System.in);
System.out.print("Please enter a filename to open:");
tempFileName = inputDevice.nextLine();
try
{
FileInputStream fis = new FileInputStream(new File (tempFileName));
ObjectInputStream ois = new ObjectInputStream(fis);
plateDB = (String[][])ois.readObject();
ois.close();
}
catch(IOException e)
{
System.err.println("Error opening file!");
}
catch(ClassNotFoundException e)
{
System.err.println("Class Not Found!");
}
return plateDB;
}
This is what I have to read in the code from the file, (Again, error handling later, like if the file does not exist).
I have tested the save code, and it does create a file with the data in it. I just can not get it to read in the code, or if it does, it does not pass the array back to the calling area. Thank you for any pointers!