I'm trying to do a simple serialization of an object. i have an entity called Student as follows:
package serialization;
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + "]";
}
public Student(int id, String name) {
super();
this.id = id;
this.name = name;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int id;
public String name;
}
and i have my main method as follows ,
package serialization;
import java.io.File;
import java.io.FileWriter;
public class Serialization {
public static void main(String args[])throws Exception{
Student s1 =new Student();
s1.setId(2);
s1.setName("hihihihihihi");
File f = new File("D://abc.txt");
FileWriter fw = new FileWriter(f);
fw.write(s1.getName().toString());
// fw.flush();
// fw.close();
System.out.println("success");
}
}
Once after writing the object in to the file in the prescribed location, i cannot see any content on the file. If i use a flush or close method , i'm able to see the name printed as "hihhihihiiii" etc.
the write method will write the object in to the stream , so why is it not present in the file.Why it is writing the data Only after closing or flushing the file Writer ??