Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
ObjectInputStream defaultReadObject() method in Java with examples
Next article icon

ObjectInputStream close() method in Java with examples

Last Updated : 28 May, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
The close() method of the ObjectInputStream class in Java closes the input stream. Syntax:
public void close()
Parameters: This method does not accept any parameter. Return Value: This method does not returns anything. Below program illustrate the above method: Program 1: Java
// 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();         }     } } 
Output: Reference: https://docs.oracle.com/javase/10/docs/api/java/io/ObjectInputStream.html#close()

Next Article
ObjectInputStream defaultReadObject() method in Java with examples
author
gopaldave
Improve
Article Tags :
  • Java
  • Java-Functions
  • Java-IO package
  • Java-ObjectInputStream
Practice Tags :
  • Java

Similar Reads

  • PushbackInputStream close() method in Java with Examples
    The close() method of PushbackInputStream class in Java is used to closes the input stream and it releases resources of system that are associated with the stream. After calling this method, further calling other method if this class will throw the IOException. Syntax: public void close() throws IOE
    2 min read
  • ObjectInputStream available() method in Java with examples
    The available() method of the ObjectInputStream class in Java returns the number of bytes that can be read without blocking the stream. Syntax: public int available() Parameters: This method does not accept any parameter. Return Value: This method returns the number of available bytes. Below program
    1 min read
  • PrintStream close() method in Java with Examples
    The close() method of PrintStream Class in Java is used to close the stream. Closing a stream deallocates any value in it or any resources associated with it. The PrintStream instance once closed won't work. Also a PrintStream instance once closed cannot be closed again. Syntax: public void close()
    2 min read
  • ObjectInputStream read() method in Java with examples
    The read() method of the ObjectInputStream class in Java reads a byte of data. This method wont run if there is no data. Syntax: public int read() Parameters: This method does not accept any parameter. Return Value: This method returns the byte read, or -1 if the end of the stream is reached. Except
    1 min read
  • ObjectInputStream defaultReadObject() method in Java with examples
    The defaultReadObject() method of the ObjectInputStream class in Java is used to read the non-static and non-transient fields of the current class from this stream. Syntax: public void defaultReadObject() Parameters: This method does not accept any parameter. Return Value: This method returns the va
    2 min read
  • ObjectInputStream readByte() method in Java with examples
    The readByte() method of the ObjectInputStream class in Java is used to read the 8 bit (byte). Syntax: public byte readByte() Parameters: This method does not accept any parameter. Return Value: This method returns the 8 bit byte read Errors and Exceptions: The function throws three exceptions which
    2 min read
  • PrintStream print(Object) method in Java with Examples
    The print(Object) method of PrintStream Class in Java is used to print the specified Object on the stream. This Object is taken as a parameter. Syntax: public void print(Object object) Parameters: This method accepts a mandatory parameter object which is the Object to be printed in the Stream. Retur
    2 min read
  • ObjectInputStream enableResolveObject() method in Java with examples
    The enableResolveObject() method of the ObjectInputStream class in Java is used to enables the stream to do replacement of objects read from the stream. When enabled, the resolveObject(java.lang.Object) method is called for every object being deserialized. Syntax: protected boolean enableResolveObje
    2 min read
  • ObjectInputStream readBoolean() method in Java with examples
    The readBoolean() method of the ObjectInputStream class in Java reads a boolean from the stream. Syntax: public boolean readBoolean() Parameters: This method does not accept any parameter. Return Value: This method returns the boolean that has been read. Errors and Exceptions: The function throws tw
    1 min read
  • PrintStream println(Object) method in Java with Examples
    The println(Object) method of PrintStream Class in Java is used to print the specified Object on the stream and then break the line. This Object is taken as a parameter. Syntax: public void println(Object object) Parameters: This method accepts a mandatory parameter object which is the Object to be
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences