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:
BufferedOutputStream write() method in Java with Examples
Next article icon

BufferedOutputStream write() method in Java with Examples

Last Updated : 05 Jun, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
  1. 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)              throws IOException  
    Overrides: This method overrides the write(int) method of FilterOutputStream class. Parameters: This method accepts b of integer type as a parameter which represents the byte to be written. Return value: This method does not return any value. Exception: This method throws IOException if an I/O error occurs. Below program illustrates write(int) method in BufferedOutputStream class in IO package: Program: Java
    // Java program to illustrate // BufferedOutputStream write(int) method import java.io.*; public class GFG {     public static void main(         String[] args) throws Exception     {          // Create byteArrayOutputStream         ByteArrayOutputStream byteArrayOutStr             = new ByteArrayOutputStream();          // Convert byteArrayOutputStream to         // bufferedOutputStream         BufferedOutputStream buffOutputStr             = new BufferedOutputStream(                 byteArrayOutStr);          for (int i = 65; i < 70; i++) {             // Writes to buffOutputStr             buffOutputStr.write(i);         }          // flush is called         // to compel bytes to be         // written out to buffOutputStr         buffOutputStr.flush();          for (             byte by : byteArrayOutStr.toByteArray()) {             // Converts byte to character             char ch = (char)by;             System.out.print(ch);         }     } } 
    Output:
      ABCDE  
  2. The write(byte[ ], int, int) method of BufferedOutputStream class in Java is used to write given length of bytes from the specified byte array starting at given offset to the buffered output stream. Basically the write() method stores bytes from the given byte array into the buffer of a stream and flushes the buffer to the main output stream. If the length is equal to the buffer of the stream then write() method flushes the buffer and writes the bytes directly to the main output stream. Syntax:
      public void write(byte[] b,                    int offset,                    int length)              throws IOException  
    Overrides: This method overrides the write(byte[ ], int, int) method in FilterOutputStream class. Parameters: This method accepts three parameters:
    • b - It is of Byte type and represents the byte array.
    • offset - It is of Integer type and represents the starting offset in the byte array.
    • length - It is of Integer type and represents the number of bytes to be written.
    Return value: This method does not return any value. Exception: This method throws IOException if an I/O error occurs. Below program illustrates write(byte[ ], int, int) method in BufferedOutputStream class in IO package: Program: Java
    // Java program to illustrate // BufferedOutputStream write( // byte[ ], int, int) method import java.io.*; public class GFG {     public static void main(         String[] args) throws Exception     {          // Create byteArrayOutputStream         ByteArrayOutputStream byteArrayOutStr             = new ByteArrayOutputStream();          // Convert byteArrayOutputStream to         // bufferedOutputStream         BufferedOutputStream buffOutputStr             = new BufferedOutputStream(                 byteArrayOutStr);          // Create byte array         byte b[] = { 71, 69, 69, 75, 83 };          // Call write(byte[ ], int, int)         // method         // It writes byte array to         // buffOutputStr         buffOutputStr.write(b, 0, 5);          // flush is called         // to compel bytes to be         // written out to buffOutputStr         buffOutputStr.flush();          for (             byte by : byteArrayOutStr.toByteArray()) {             // Converts byte to character             char ch = (char)by;             System.out.print(ch);         }     } } 
    Output:
      GEEKS  
References:
  • https://docs.oracle.com/javase/10/docs/api/java/io/BufferedOutputStream.html#write(int)
  • https://docs.oracle.com/javase/10/docs/api/java/io/BufferedOutputStream.html#write(byte%5B%5D, int, int)

Next Article
BufferedOutputStream write() method in Java with Examples

P

pp_pankaj
Improve
Article Tags :
  • Java
  • Java-Functions
  • Java-IO package
Practice Tags :
  • Java

Similar Reads

    ByteArrayOutputStream write() method in Java with Examples
    The write() method of ByteArrayOutputStream class in Java is used in two ways: 1. The write(int) method of ByteArrayOutputStream class in Java is used to write the specified byte to the ByteArrayOutputStream. This specified byte is passed as integer type parameter in this write() method. This write(
    2 min read
    BufferedInputStream reset() method in Java with Examples
    The reset() method of BufferedInputStream class in Java is used to reset the position of the stream to the position at the time the mark method was last called. It is used with the combination of mark() method of the same class. General Contract: There are two cases: If mark() and reset() are suppor
    3 min read
    BufferedInputStream read() method in Java with Examples
    read() method of BufferedInputStream class in Java is used to read the next byte of data from the input stream. When this read() method is called on the input stream then this read() method reads one character of the input stream at a time. Syntax: public int read() Overrides: It overrides read() me
    3 min read
    BufferedInputStream skip(long) method in Java with Examples
    The skip(long) method of BufferedInputStream class in Java is used to skip n bytes of data from the buffered input stream. The number of bytes skipped is stored and returned as long type. The termination condition involves either of the two: Either reading into a byte array until n-bytes are covered
    2 min read
    Buffer duplicate() method in Java with Examples
    The duplicate() method of java.nio.Buffer class is used to create a new buffer that shares this buffer's content. The content of the new buffer will be that of this buffer. Changes to this buffer's content will be visible in the new buffer, and vice versa; the two buffers' position, limit, and mark
    3 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