当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java ObjectInputStream close()用法及代码示例


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

用法

public void close()

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


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

以下示例程序旨在说明上述方法:

示例1:

// 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://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#close()



相关用法


注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 ObjectInputStream close() method in Java with examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。