BufferedOutputStream class in java

java.io.BufferedOutputStream is a class in java programming language. Inside of this class exists 3 public methods. This class are providing the functionality to write text content into file. Declaration of this class as follows.

public class java.io.BufferedOutputStream 
extends java.io.FilterOutputStream

BufferedOutputStream public method

There are following useful methods which is define the inside of java.io.BufferedOutputStream class.

MethodDescription
void flush() It flushes this buffered output stream.
void write(int) It writes the specified byte to this buffered output stream.
void write(byte[], int, int) It writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Public Constructors

public java.io.BufferedOutputStream(java.io.OutputStream)
public java.io.BufferedOutputStream(java.io.OutputStream,int)
// Java program for java.io.BufferedOutputStream class flush() method
// Useful packages
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
class Example
{
	public static void main(String[] args)
	{
		// Create Instance
		try
		{
			// data.txt is file in current directory
			FileOutputStream f1 = new FileOutputStream("data1.txt");
			FileOutputStream f2 = new FileOutputStream("data2.txt");

			// Create an instance of BufferedOutputStream
			BufferedOutputStream x = new BufferedOutputStream(f1);
			BufferedOutputStream y = new BufferedOutputStream(f2,10);
			// Write value
			// Input ASCII value
			x.write(72); // H
			x.write(97); // a
			x.write(112); // p
			x.write(112); // p
			x.write(121); // y
			x.flush();
			// or
			y.write(' '); // 32
			y.write('?'); // 63
			y.flush(); 
	
		
			f1.close();
			x.close();
			f2.close();
			y.close();


			// data1.txt : Happy 
			// data2.txt :  ?

			System.out.println("Submit");
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
}
BufferedOutputStream class instance in java
Submit

java.io.BufferedOutputStream write(byte[], int, int) method example

// Java program for java.io.BufferedOutputStream class flush() method
// Useful packages
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
class Example
{
	public static void main(String[] args)
	{
		// Create Instance
		try
		{
			// data.txt is file in current directory
			FileOutputStream f = new FileOutputStream("data.txt");
			

			// Create an instance of BufferedOutputStream
			BufferedOutputStream x = new BufferedOutputStream(f,10);

			byte [] info = {65,90,97,101,105,65,66,76};
			// Write value
			x.write(info,2,4); 
			x.flush();

			x.close();
			f.close();
			System.out.println("Submit"); // aeiA
			
		}
		catch (Exception e)
		{
			System.out.println(e);
		}
	}
}
Submit


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved