I know I'm making a mistake, but I'm wondering if someone could tell me the type of mistake I'm making. I know I can write the whole array to a file, but I want to create separate binary files for each Customer. Then I want to readout the binary file... either one or all of them.
Could someone help me? Thanks.
Could someone help me? Thanks.
Code:
Customer[] oneArray = new Customer[2];
oneArray[0] = new Customer("John", "email@gmail.com", "123-345-1234", "1, 2");
oneArray[1] = new Customer("Smith", "email2@gmail.com", "234-456-1234", "1, 2, 3");
String fileName1 = "Data/Customers/John.dat";
String fileName2 = "Data/Customers/Smith.dat";
try
{
ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(fileName1));
outputStream.writeObject(oneArray[0]);
outputStream.close();
ObjectOutputStream outputStream2 = new ObjectOutputStream(new FileOutputStream(fileName2));
outputStream2.writeObject(oneArray[1]);
outputStream2.close();
}
catch(IOException e)
{
System.out.println("Error writing to file.");
System.exit(0);
}
Customer[] anotherArray = null;
try
{
ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName1));
anotherArray[0] = (Customer)inputStream.readObject();
inputStream.close();
ObjectInputStream inputStream2 = new ObjectInputStream(new FileInputStream(fileName2));
anotherArray[1] = (Customer)inputStream2.readObject();
inputStream2.close();
}
catch(Exception e)
{
System.out.println("Error reading file.");
System.exit(0);
}
System.out.println(anotherArray[0]);
System.out.println(anotherArray[1]);
Comment