When to use flush() in java?

前端 未结 6 1780
长发绾君心
长发绾君心 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:20

    1. You use flush() to clear the internal buffers(if any) and force write(i.e. flush) all the pending data in to the stream destination.
    2. close() method is used to close the stream, once called, you can't use the stream any more.
    3. No. You are reading "a John object from a file". An object of type John should have been previously serialized in to the file.
    4. There are many alternatives to Java serialization such as XStream. See this SO answer for alternatives to serialization.

提交回复
热议问题