Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • 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
  • 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:
Java BufferedOutputStream Class
Next article icon

Java BufferedOutputStream Class

Last Updated : 07 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

BufferedOutputStream class in Java is a part of the java.io package. It improves the efficiency of writing data to an output stream by buffering the data. This reduces the number of direct writes to the underlying output stream, making the process faster and more efficient.

Example 1: The below Java program demonstrates how to use the bufferedOutputStream class to efficiently write data to a file by buffering the output.

Java
// Writing data to a file // using BufferedOutputStream import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException;  public class Geeks {     public static void main(String[] args)     {         String s             = "This is an example of BufferedOutputStream.";          try (FileOutputStream fileOutputStream              = new FileOutputStream("output.txt");              BufferedOutputStream bufferedOutputStream              = new BufferedOutputStream(fileOutputStream)) {              // Convert string to bytes and write to the             // buffered output stream             bufferedOutputStream.write(s.getBytes());              System.out.println(                 "Data written to the file successfully.");         }         catch (IOException e) {             e.printStackTrace();         }     } } 

Output
Data written to the file successfully. 

Declaration of BufferedOutputStream Class

public class BufferedOutputStream extends FilterOutputStream

Constructor of BufferedOutputStream class

  • BufferedOutputStream(OutputStream out): Creates a buffered output stream with a default buffer size of 8192 bytes.
  • BufferedOutputStream(OutputStream out, int size): Creates a buffered output stream with the specified buffer size.

Methods of BufferedOutputStream Class

Method

Action performed

write(int b)

Writes the specified byte to this buffered output stream

write(byte[] b, int off, int len)

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

flush()

Forces any buffered output bytes to be written out

Example 2: The below Java program demonstrates how to use BufferedOutputStream class to write data to a file effectively by buffering output using methods like write(), flush() and close()

Java
// Java program demonstrating BufferedOutputStream import java.io.*;  class Geeks {     public static void main(String args[]) throws Exception     {         FileOutputStream fout             = new FileOutputStream("f1.txt");          // creating bufferdOutputStream obj         BufferedOutputStream bout             = new BufferedOutputStream(fout);          // illustrating write() method         for (int i = 65; i < 75; i++) {             bout.write(i);         }          byte b[] = { 75, 76, 77, 78, 79, 80 };         bout.write(b);          // illustrating flush() method         bout.flush();          // illustrating close() method         bout.close();         fout.close();     } } 

Output:

Output

Note: A file named f1.txt is created in the current working directory and the file will contain ABCDEFGHIJKLMNOP.


Next Article
Java BufferedOutputStream Class

N

Nishant Sharma
Improve
Article Tags :
  • Java
  • Java-Classes
  • Java-IO package
Practice Tags :
  • Java

Similar Reads

    Java.io.BufferedInputStream class in Java
    A BufferedInputStream adds functionality to another input stream-namely, the ability to buffer the input and to support the mark and reset methods. When the BufferedInputStream is created, an internal buffer array is created. As bytes from the stream are read or skipped, the internal buffer is refil
    4 min read
    FileOutputStream in Java
    In Java, the FileOutputStream class is a subclass of OutputStream. It is used to write data to a file as a stream of bytes. FileOutputStream is commonly employed for writing primitive values into a file. FileOutputStream supports writing both byte-oriented and character-oriented data.Note: FileWrite
    7 min read
    Java.io.ByteArrayOutputStream() Class in Java
    java.io.ByteArrayOutputStream class creates an Output Stream for writing data into byte array. The size of buffer grows automatically as data is written to it. There is no affect of closing the byteArrayOutputStream on the working of it's methods. They can be called even after closing the class. Thu
    4 min read
    BufferedOutputStream write() method in Java with Examples
    The write(int) method of BufferedOutputStream class in Java is used to write the specified byte to the buffered output stream. The specified byte is passed as an integer to the write() method here. It is used to write one byte as a time to the BufferedOutputStream. Syntax: public void write(int b) t
    3 min read
    BufferedInputStream close() method in Java with Examples
    The close() method of BufferedInputStream class in Java closes the input stream and releases any system resources associated with it. Once the close() method is called, reading from any input file is banned and the system will throw an IOException. To tackle the issue, the user might use a try-catch
    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