This example illustrates using ObjectOutputStream to write an object to a file. This is a part of serialization tutorial.
fout=new FileOutputStream("obj.dat"): This statement creates a FileOutputStream object pointing to obj.dat
out=new ObjectOutputStream(fout): This statement creates an ObjectOutputStream object pointing to the given FileOutputStream object.
writeObject(st): This method in the ObjectOutputStream class writes a String object to the file obj.dat
import java.io.*;
class WriteObj
{
public static void main(String args[]) throws Exception
{
FileOutputStream fout=new FileOutputStream("obj.dat");
ObjectOutputStream out=new ObjectOutputStream(fout);
String st="I am written in the file.";
out.writeObject(st);
out.close();
fout.close();
}
}
fout=new FileOutputStream("obj.dat"): This statement creates a FileOutputStream object pointing to obj.dat
out=new ObjectOutputStream(fout): This statement creates an ObjectOutputStream object pointing to the given FileOutputStream object.
writeObject(st): This method in the ObjectOutputStream class writes a String object to the file obj.dat