How to create a class that creates a instance of the UserInfo class and writes the object to an output stream?
Submitted by: AdministratorCreate a class that creates a instance of the UserInfo class and writes the object to an output stream as shown in Code Sample. The output stream in this example is a file called "name.out". The important thing to note from Code Sample is that the writeObject method can be called any number of times to write any number of objects to the output stream.
Code Sample: SaveInfo.java
import java.io.*;
import java.util.Date;
public class SaveInfo {
public static void main
(String argv[]) throws Exception {
FileOutputStream fos =
new FileOutputStream("name.out");
ObjectOutputStream oos =
new ObjectOutputStream(fos);
// create two objects
UserInfo user1 = new UserInfo("Java Duke");
UserInfo user2 = new UserInfo("Java Blue");
// write the objects to the output stream
oos.writeObject(user1);
oos.writeObject(user2);
oos.flush();
oos.close();
fos.close();
}
}
Submitted by: Administrator
Code Sample: SaveInfo.java
import java.io.*;
import java.util.Date;
public class SaveInfo {
public static void main
(String argv[]) throws Exception {
FileOutputStream fos =
new FileOutputStream("name.out");
ObjectOutputStream oos =
new ObjectOutputStream(fos);
// create two objects
UserInfo user1 = new UserInfo("Java Duke");
UserInfo user2 = new UserInfo("Java Blue");
// write the objects to the output stream
oos.writeObject(user1);
oos.writeObject(user2);
oos.flush();
oos.close();
fos.close();
}
}
Submitted by: Administrator
Read Online Socket Programming Job Interview Questions And Answers
Top Socket Programming Questions
☺ | Socket Programming Interview Questions |
☺ | How does the race condition occur? |
☺ | What Is Socket? |
☺ | Socket Programming in C using TCP with Code? |
☺ | What is the difference between TCP and UDP? |
Top Coding/Programming Categories
☺ | Python Interview Questions. |
☺ | OOP Interview Questions. |
☺ | Software engineering Interview Questions. |
☺ | PHP Interview Questions. |
☺ | VBA (Visual Basic for Applications) Interview Questions. |