I have a file with an arraylist based on the class Car for example
but I also have files with an arraylist based on the class Driver, and other files like that
I use following code to load such an arraylist:
import java.io.*;
import java.util.*;
public class FileInterface {
private ArrayList<Car> cars;
// The name of the file where the highcar will be saved
private static final String FILE = "cars.dat";
//Initialising an in and outputStream for working with the file
ObjectOutputStream outputStream = null;
ObjectInputStream inputStream = null;
public FileInterface() {
//initialising the car-arraylist
cars = new ArrayList<Car>();
}
public void loadScoreFile() {
try {
inputStream = new ObjectInputStream(new FileInputStream(HIGHSCORE_FILE));
cars = (ArrayList<Car>) inputStream.readObject();
} catch (FileNotFoundException e) {
System.out.println("[Load] FNF Error: " + e.getMessage());
} catch (IOException e) {
System.out.println("[Load] IO Error: " + e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("[Load] CNF Error: " + e.getMessage());
} finally {
try {
if (outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (IOException e) {
System.out.println("[Laod] IO Error: " + e.getMessage());
}
}
}
}
is there a way that I can make the loadfunction work for every type of arraylist? (ArrayList<Driver>, ArrayList<Car>, ...)
it seems stupid to put that piece of code multiple times just because it has to load a different kind of ArrayList


Sign In
Create Account


Back to top









