When to use flush() in java?

前端 未结 6 1811
长发绾君心
长发绾君心 2020-12-14 04:53
import java.io. * ;
public class Ser {

    public static void main(String args[]) {

        try {
            John myObj = new John(\"Sachin\", \"Cricket\");
              


        
6条回答
  •  悲哀的现实
    2020-12-14 05:36

    1. When to use flush method and why do we use it?

    This is used when there needs to be synchronous sending

    For example you have a duplex (2-way) connection and you just sent a message and now need to wait for the reply on it, without flush a buffered outputstream might hold it until the buffer fills up (deadlock)

    1. myObj2 = (John) ois.readObject(); Please correct me if I am wrong, I am reading the file object and storing into another object and typecasting the file object.

    WRONG! You read some data from the stream typecast it into a John (which will throw if the object read wasn't a John)

    1. What are the alternatives of Serialization or persisting the data in Java. I don't want the data to get into file as byte stream.

    You can write it as text (write out the fields as text on a line) and provide a method to parse it into an object

提交回复
热议问题