Java BufferedOutputStream


 

Java BufferedOutputStream

This section demonstrates you the use of BufferedOutputStream class.

This section demonstrates you the use of BufferedOutputStream class.

Java BufferedOutputStream

This section demonstrates you the use of BufferedOutputStream class.

Using BufferedOutputStream class, you can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written.

In the given example, we have created an object of BufferedOutputStream class and using the object of this class, we have called the method write(). This method takes either a byte array or an int as argument. Now to write the string using this method, we have called getBytes() method on the specified string.
getBytes(): This method of String class returns its content in the form of a byte array.

Here is the code:

import java.io.*;

public class BufferedOutputStreamExample {
	public static void main(String[] args) throws Exception {
		BufferedOutputStream bos = new BufferedOutputStream(
				new FileOutputStream("C:/newFile.txt"));
		String st = "Hello World";
		bos.write(st.getBytes());
		bos.close();
	}
}

Ads