Issue
/**
* A method that writes variables in a file for shows in the program.
*
* @author
* @version (1.2 2022.01.05)
*/
public void writeShowData(String fileName) throws FileNotFoundException{
String showPath = "c:\\Users\\COMPUTER\\bluej files\\projects\\chapter15\\willem\\CinemaReservationSystemFiles\\";
try
{
FileOutputStream fos = new FileOutputStream(showPath+fileName,true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(show);
oos.flush();
oos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* A method that reads variables in a file for shows setup use in the program.
*
* @author
* @version (1.2 2022.01.05)
*/
public void readShowData(String fileName) throws FileNotFoundException {
try
{ FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
List <Show>shows =(List<Show>)ois.readObject();
ois.close();
System.out.println(shows.toString());// for testing
}
catch (IOException e) {
e.printStackTrace();
}catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
the line : List <Show> shows= (List<Show>)ois.readObject();
gives a
ClassCastexception :class Show cannot be cast to class java.util.List (etc etc....
I tried many things but can't get it working well.
I made the class show and related classes
Serializable
and then write it to shows.ser but when it reads it goes wrong.
Who can help me out on this?
Solution
here is my latest version that is working, the new code does leave out a few lines like if(file.exists){file.delete()}.etc... because the used streams doing that automaticly. i totally overwrite the file(s) with an update method instead of writing one object at a time. The earlier answers and some testing by myself made me understand how to get it done right for the purposes in my code.
/**
* A method that writes/saves variables of an Arraylist in a file for seatReservations in the program.
*
* @author Willem
* @version (1.6 2022.01.20)
*/
public void overWriteShowData(String fileName1) throws FileNotFoundException {
String seatReservationPath = "c:\\Users\\COMPUTER\\bluej files\\projects\\chapter15\\willem\\CinemaReservationSystemFiles\\";
try
{ File file = new File(seatReservationPath + fileName1);
ObjectOutputStream oos;
FileOutputStream fos = new FileOutputStream(file.getCanonicalPath(),false); // regular fos
oos = new ObjectOutputStream(fos);
oos.writeObject(shows);
oos.flush();
oos.close();
}catch (IOException e) {
e.printStackTrace();
}
}
/**
* A method that reads variables in a file for shows setup use in the program.
*
* @author
* @version (1.6 2022.01.20)
*/
public void readShowData(String fileName1) throws IOException, EOFException,ClassNotFoundException{
FileInputStream fis = new FileInputStream(fileName1);
ObjectInputStream ois = new ObjectInputStream(fis);
shows = (List<Show>) ois.readObject();
ois.close();
}
private void updateAllData(String fileName) {
try {
if(fileName.equals("theatres.ser"))
{overWriteTheatreData(fileName);}
if(fileName.equals("seatPlans.ser"))
{overWriteSeatPlanData(fileName);}
if(fileName.equals("shows.ser"))
{overWriteShowData(fileName);}
if(fileName.equals("customers.ser"))
{overWriteCustomerData(fileName);}
if(fileName.equals("seatReservations.ser"))
{overWriteSeatReservationData(fileName);}
} catch (FileNotFoundException fnfe) {
//fnfe.printStackTrace();
CinemaReservationSystemGUI.cannotFindFileMessage(fileName);
}
}
Answered By - Willem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.