Java 字节数组输出流
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((char)byt[i] + "* * *"); } System.out.println(" "); } }
Output:
In this program, a byteoutputstream is created, and each of the characters gets printed.
Example #2
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.
Example #3
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.
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.
结论
Java ByteArrayOutputStream 是一个类,它可以借助字节数组将公共数据写入多个文件。这里,流所拥有的数据副本被转发到多个流。本文详细讲解了Java中ByteArrayOutputStream的声明、语法、构造函数、方法、工作原理以及实例等几个细节。
以上是Java 字节数组输出流的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。
