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
Below are the steps to create a ByteArrayOutputStream in Java.
ByteArrayOutputStream bobj = new ByteArrayOutputStream() ;
or
ByteArrayOutputStream bobj = new ByteArrayOutputStream(int n) ;
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.
The following are the different methods that perform several functions.
Now, let us see some of the sample examples.
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((char)byt[i] + "* * *"); } System.out.println(" "); } }
Output:
In this program, a byteoutputstream is created, and each of the characters gets printed.
Java program to print the data from a byte array and conversion of characters to upper case.
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( ( char ) byt [ i ] + "* * *"); } System.out.println(" "); int ch; ByteArrayInputStream biobj = new ByteArrayInputStream(byt); System.out.println("Conversion of each character to Upper case " ); for(int j = 0 ; j< 1; j++ ) { while( ( ch = biobj.read( ) ) != - 1) { //convert character to upper case System.out.println(Character.toUpperCase( ( char ) ch ) ); } biobj.reset( ); } } }
Output:
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.
Java program to copy the content of a text file to another file.
Code:
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.
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.
Java ByteArrayOutputStream 是一個類,它可以藉助位元組數組將公共資料寫入多個檔案。這裡,流所擁有的資料副本被轉送到多個流。本文詳細說明了Java中ByteArrayOutputStream的宣告、語法、建構子、方法、運作原理、以及實例等幾個細節。
以上是Java 位元組數組輸出流的詳細內容。更多資訊請關注PHP中文網其他相關文章!