Advertisement
ByteArrayOutputStream() |
---|
This constructor creates a ByteArrayOutputStream object for writing out the contents of a byte array.
Example-:
|
Methods | Description |
---|---|
flush() | This method flushes any buffered data to be written out of this output stream. |
size() | This method returns the current size of the buffer of ByteArrayOutputStream. |
write(int b) | This method writes one byte at a time to this output stream. |
write(byte[]) | This method writes a whole byte array at a time to this input stream. |
writeTo(OutputStream out) | This method writes the content of ByteArrayOutputStream to the output stream passed to it in the parameters. |
close() | This method closes this output stream and also frees any resources connected with this output stream. |
Advertisement
// A program to write a byte at a time using ByteArrayOutputStream.
import java.io.*;
class A
{
public static void main(String... ar)
{
try
{
String str= new String("HelloJava");
byte b[]=str.getBytes();
FileOutputStream fos=new FileOutputStream("D:/TextBook.txt");
ByteArrayOutputStream baos= new ByteArrayOutputStream();
//Initial buffer size of ByteArrayOutputStream is 0
System.out.println("Initial Size of the buffer"+ baos.size());
//Writing one byte at a time to ByteArrayOutputStream's buffer
for(byte byt : b)
{
baos.write(byt);
}
System.out.println("Content written to ByteArrayOutputStream's buffer = "+baos.toString());
System.out.println("Size of the buffer after writing data to it = "+ baos.size());
//writing out the contents of BufferArrayOutputStream's buffer to a file.
baos.writeTo(fos);
//flusing all the bytes out of output stream
baos.flush();
fos.flush();
fos.close();
}
catch(IOException e)
{
System.out.println(e);
}
}
}
Initial Size of the buffer = 0
Content written to ByteArrayOutputStream's buffer = HelloJava
Size of the buffer after writing data to it = 9
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement