Now I am creating an address book in Java 1.6. Now how I have it set up is when you add a contact it gets saved into an array. I have no problem writing the array, however when it comes to reading it I dont know how to get every object I previously saved, and load it into the array again.
Just do you know: addbook is my file, it is a txt file. Array is the array I am using to store the objects. The sort I am using is an insertion sort that sorts the contacts by name. If I have not covered other variable names, and stuff like that, I can clarify.
Finally, just for clarification, my question is asking how I can read the array I saved to a file.
Here is my read code:
try {
FileInputStream in = new FileInputStream(addBook);
ObjectInputStream readIn = new ObjectInputStream(in);
array = readIn.readObject();
readIn.close();
Sorts.insertionSort(array);
model.removeAllElements();
for (int i = array.length - 1; i > 0; i--) {
model.addElement(((Book) array[i]).getContact());
}
comboBox.setModel(model);
} catch (Exception e) {
e.printStackTrace();
}
ObjectInputStream.readObjectreturns anObject. You cannot assign anObjectto aComparable[]without a cast:array = (Comparable[]) readIn.readObject().