Java problem The code below tries to store the ArrayList of Car objects in a fil
ID: 3851730 • Letter: J
Question
Java problem The code below tries to store the ArrayList of Car objects in a file, but the file turns out to be empty. The problem is that Java does not know whether or not to write objects of type Car to a file. What interface should the Car class implement to fix this problem? import java.io.*: import java.util.ArrayList: class Car { String make: String model: public Car(String make. String model) { this.make = make: this.model = model: } } public class Question { public static void main(String[] args) throws Exception { ArrayList fleet = new ArrayList (): fleet.addCar ("Honda", "Civic"): fleet.addCar("Ford", "Taurus"): ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("fleet.dat"))): oos.writeObject(fleet): oos.close(): } }Explanation / Answer
////Program output will be feelt.txt file and if u opened it will show in byte code and to get proper content we should Deserialize above program/////
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
class Car implements Serializable {
String make;
String model;
public Car(String make,String model) { this.make=make;
this.model=model;
}
}
public class Question {
public static void main(String[] args) throws Exception {
ArrayList fleet=new ArrayList(); fleet.add(new Car("Honda", "Civic")); fleet.add(new Car("Ford", "Taurus")); FileOutputStream fos =new
FileOutputStream("fleet.txt"); ObjectOutputStream out =new ObjectOutputStream(fos); out.writeObject(fleet); out.close(); fos.close(); }
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.