📌  相关文章
📜  Java中的 ObjectInputStream close() 方法及示例

📅  最后修改于: 2022-05-13 01:54:33.713000             🧑  作者: Mango

Java中的 ObjectInputStream close() 方法及示例

Java中ObjectInputStream类的close()方法关闭输入流。

语法

public void close()

参数:此方法不接受任何参数。

返回值:此方法不返回任何内容。

下面的程序说明了上述方法:

方案一:

// Java program to illustrate 
// the above method
  
import java.io.*;
  
public class GFG {
  
    public static void main(String[] args) throws Exception
    {
  
        try {
            // Creates a new file
            // with an ObjectOutputStream
            FileOutputStream out
                = new FileOutputStream("gopal.txt");
            ObjectOutputStream out1
                = new ObjectOutputStream(out);
  
            // write in the file
            out1.writeUTF("Geeks for Geeks");
  
            // FLushes the stream
            out1.flush();
  
            // Create an ObjectInputStream
            // for the file we created before
            ObjectInputStream example
                = new ObjectInputStream(
                    new FileInputStream("gopal.txt"));
  
            // read from the stream
            for (int i = 0; i < example.available();) {
                System.out.println("" + (char)example.read());
            }
  
            // closes the stream
            System.out.println("\nClosing Stream...");
  
            example.close();
  
            // Stream closed
            System.out.println("Stream Closed.");
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}

输出:

参考:https: Java/io/ObjectInputStream.html#close()