-3

I need help writing an object to a file in Java with this code.

FileOutputStream objectFOS = new FileOutputStream("Items.dat");
DataOutputStream objectDOS = new DataOutputStream(objectFOS);
objectDOS.writeObject(one);

one is of class Item that I defined, and the class implements Serializable. I keep getting the error message:

Error: cannot find symbol
symbol:   method writeObject(Item)
location: variable objectDOS of type java.io.DataOutputStream
user207421
  • 305,947
  • 44
  • 307
  • 483
Gideon
  • 37
  • 1
  • 9

1 Answers1

1

DataOutputStream supports only primitive datatype. To write an object , you may use ObjectOutputStream.

FileOutputStream objectFOS = new FileOutputStream("Items.dat");
ObjectOutputStream objectDOS = new ObjectOutputStream(objectFOS);
objectDOS.writeObject(one);
Sreehari B S
  • 48
  • 1
  • 7