I have an arrayList of objects public ArrayList<Brick> bricks;
defined later in the constructor bricks = new ArrayList<Brick>();
. And then used in paintComponent method:
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g);
for(Brick b: bricks){
b.draw(g2d, b.type);
}
Toolkit.getDefaultToolkit().sync();
}
After adding bunch of objects I'm using serialize method :
public void serialize(ArrayList<Brick> al){
try{
FileOutputStream fos= new FileOutputStream("levels/level1");
ObjectOutputStream oos= new ObjectOutputStream(fos);
oos.writeObject(al);
oos.close();
fos.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
to save it to file (Although Eclipse throws some warnings the file is being created). However I'm having problem deserializing it (list is not being loaded):
public void deserialize(){
try
{
FileInputStream fis = new FileInputStream("levels/level1");
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Brick> readObject = (ArrayList<Brick>) ois.readObject();
bricks = readObject;
ois.close();
fis.close();
}catch(IOException ioe){
ioe.printStackTrace();
return;
}catch(ClassNotFoundException c){
System.out.println("Class not found");
c.printStackTrace();
return;
}
}
Am I doing something wrong ?