Write and read object to and from a file

Hi, i am Java beginner. I know how to write and read text to and from a text file using FileWriter and FileReader. However, they are not capable of writing and reading object to and from a file. What classes should i use to achieve this goal? please help.

By sakara99 asked on 2015-12-17
answer to this question

By yuk.dara

To write an object to a file (serialization), you use ObjectOutputStream wrapping a FileOutputStream object. To read the object back from the file (deserialization), you use ObjectInputStream wrapping a FileInputStream object. The object to be written to the file must implement the Serializable interface.

class Customer implements Serializable{

public static void writeObject(Customer obj){
FileOutputStream fos=new FileOutputStream("test.bin");
ObjectOutputStream oos=new ObjectOutputStream(fos);
}catch(IOException ie){}


public static Customer readObject(){
Customer Object=null;
FileInputStream fis=new FileInputStream("test.bin");
ObjectInputStream ois=new ObjectInputStream(fis);

}catch(Exception ie){}
return Object;


2015-12-17 Reply

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.