In Java, ByteArrayOutputStream is a class that helps in writing common data into more than one file. Here, a byte array is used in order to write data that helps in writing data into multiple files. This stream holds a data copy and forwards the data into several streams. Based on the size of the data, the stream gets automatically larger. This class gets inherited from the package Java.io.ByteArrayOutputStream.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Declaration of Java ByteArrayOutputStream class
Below is the declaration:
public class ByteArrayOutputStream extends OutputStream
Syntax
Following is the syntax:
ByteArrayOutputStream bobj = new ByteArrayOutputStream() ; // A ByteArrayOutputStream with default size will get created ByteArrayOutputStream bobj = new ByteArrayOutputStream(int n) ; // A ByteArrayOutputStream with size n will get created
How ByteArrayOutputStream classwork in Java?
Below are the steps to create a ByteArrayOutputStream in Java.
- Import the package java.io.ByteArrayOutputStream.
- Create an output stream as shown below.
ByteArrayOutputStream bobj = new ByteArrayOutputStream() ;
or
ByteArrayOutputStream bobj = new ByteArrayOutputStream(int n) ;
- Perform different functionalities based on the requirement using the methods mentioned in the below sections.
Constructor
Java ByteArrayOutputStream has two constructors. They are:
ByteArrayOutputStream ( ): A ByteArrayOutputStream with default size will get created even though the size of the buffer gets increased if necessary.
ByteArrayOutputStream(int n):A ByteArrayOutputStream with size n bytes will get created.
Method
The following are the different methods that perform several functions.
- int size() : The current buffer size will be returned on calling this method.
- byte[] toByteArray(): A newly allocated array in bytes will get created.
- String toString(): By decoding bytes with the help of a default character set in the platform, data will be converted into a string.
- String toString(String cname): By decoding bytes with the help of character set cname in the platform, data will be converted into a string.
- void write(int byt): Byte byt will be written to the ByteArrayOutputStream.
- void write(byte[] byt , int offstart, int n): Byte byt of size n will be written to the ByteArrayOutputStream where the offset starts from offstart.
- void writeTo(OutputStream o): The whole data of a byte array will be written to the ByteArrayOutputStreamO.
- void reset(): Count field of the byte array will be reset to the value zero.
- void close(): ByteArrayOutputStream will be closed on calling this method.
Examples of Java ByteArrayOutputStream
Now, let us see some of the sample examples.
Example #1
Java program to print the data from a byte array
Code:
import java.io.*; //class public class ByteExample { //main method public static void main(String args[])throws IOException { //create an object for the bytearrayoutputstream ByteArrayOutputStream bobj = new ByteArrayOutputStream(20); while( bobj.size()!= 15 ) { bobj.write("happy".getBytes()); } byte byt [] = bobj.toByteArray(); System.out.println("The content gets printed as: "); for(int i = 0; i<byt.length i print the characters system.out.print system.out.println> <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500537138748.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java ByteArrayOutputStream" ></p> <p>In this program, a byteoutputstream is created, and each of the characters gets printed.</p> <h4 id="Example">Example #2</h4> <p>Java program to print the data from a byte array and conversion of characters to upper case.</p> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">import java.io.*; //class public class ByteExample { //main method public static void main(String args[])throws IOException { //create an object for the bytearrayoutputstream ByteArrayOutputStream bobj = new ByteArrayOutputStream(20); while( bobj.size()!= 15 ) { bobj.write("happy".getBytes()); } byte byt [] = bobj.toByteArray(); System.out.println("The content gets printed as: "); for(int i = 0; i<byt.length i print the characters system.out.print char byt system.out.println int ch bytearrayinputstream biobj="new" of each character to upper case for j="0" while biobj.reset> <p><strong>Output:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500537227770.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java ByteArrayOutputStream" ></p> <p>In this program also, a byteoutputstream is created, and each of the characters gets printed separated with *. Moreover, each character of the byte array gets converted to an upper case and printed.</p> <h4 id="Example">Example #3</h4> <p>Java program to copy the content of a text file to another file.</p> <p><strong>Code:</strong></p> <pre class="brush:php;toolbar:false">import java.io.*; //class public class ByteExample { //main method public static void main(String args[])throws Exception { //create fileoutputstreams 1 and 2 FileOutputStream fobj1=new FileOutputStream("F:\\EduCBA\\May\\byte1.txt"); FileOutputStream fobj2=new FileOutputStream("F:\\EduCBA\\May\\byte2.txt"); //create bytearrayoutputstream ByteArrayOutputStream bobj=new ByteArrayOutputStream(); //write the content bobj.write(100); //write to the text files bobj.writeTo(fobj1); bobj.writeTo(fobj2); bobj.flush() ; // bobj.close() ; System.out.println("Operation runs successfully..."); } }
Output:
In this program, two files byte1 and byte2, are created for copying the content from byte1 to byte2.
On executing the code, the content of the byte1 files gets copied to byte2.
Moreover, a line Operations runs successfully also gets printed in the console.
Example #4
Program to implement bytearrayoutputstream using the methods toByteArray() and write(buff, offset, maximum length).
Code:
import java.io.*; //class public class ByteExample { //main method public static void main(String[] args) throws IOException { // ByteArrayOutputStream bobj = new ByteArrayOutputStream(); byte[] buff = { 'H', 'A' , 'P' , 'P' , 'Y' }; //write to the buffer bobj.write(buff, 0, 5); System.out.print(" write ( buffER , off set, max len) by toByteArray( ) usage : "); // toByteArray() method usage for ( byte byt: bobj.toByteArray( ) ) { System.out.print("*" + byt ); } } }
Output:
In this program, the usage of different methods such as toArray( ) and write( buff, offset, maximum length) is explained.
Conclusion
Java ByteArrayOutputStream is a class that helps in writing common data into more than one file with the help of a byte array. Here, a data copyholds by the stream is forwarded to several streams. In this article, several details such as declaration, syntax, constructor, methods, working, and practical examples of ByteArrayOutputStream in Java is explained in detail.
The above is the detailed content of Java ByteArrayOutputStream. For more information, please follow other related articles on the PHP Chinese website!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
