當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。