Signup/Sign In

Java BufferedWriter write() Method

In this tutorial, we will learn about write() method from the BufferedWriter class in Java. This method is used to write the data to the BufferedWriter. We have three overloading method of this method:

Syntax

This method is used to write a portion of a string with the specified start and end index.

void write(String s, int off, int len)

This method is used to write a portion of an array of characters.

void write(char[] cbuf, int off, int len)

This method is used to write a single character.

void write(int c)

Example of write() Method

Here in this example, we are passing three parameters to the function as a string which is the source to be written then there is an offset as a second parameter which indicates the starting point of string from where we will start reading and the third parameter is the length of the string from where we will start reading. Here we have a String "Hello Studytonight" and if we count 8 positions from the index 2 then it will write the text "llo Stud" to the file.

import java.io.BufferedWriter;
import java.io.FileWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			String str = "Hello Studytonight";
			FileWriter writer = new FileWriter("E:\\studytonight\\output.txt");  
			BufferedWriter buffer = new BufferedWriter(writer);  
			buffer.write(str, 2, 8);  
			buffer.close();  
			System.out.println("Data is written to the file successfully..."); 
		}
		catch(Exception e)
		{
			System.out.print(false);
		}
	}
}


Data is written to the file successfully...

output.txt

llo Stud

Example 2

This example is similar to the example given above but the difference is here we are passing an array of characters as the source then offset as an index from where we will start reading the text and length of the data to be written to the BufferedWriter.

import java.io.BufferedWriter;
import java.io.FileWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			char arr[] = {'H', 'e', 'l', 'l', 'o', 'S', 't', 'u', 'd', 'y', 't', 'o', 'n', 'i', 'g', 'h', 't'};
			FileWriter writer = new FileWriter("E:\\studytonight\\output.txt");  
			BufferedWriter buffer = new BufferedWriter(writer);  
			buffer.write(arr, 2, 8);  
			buffer.close();  
			System.out.println("Data is written to the file successfully..."); 
		}
		catch(Exception e)
		{
			System.out.print(false);
		}
	}
}


Data is written to the file successfully...

output.txt

lloStudy

Example of write(int) Method

In this example, we are passing an integer as a source of data as an ASCII code for that particular character, and this method will write that ASCII code to the BufferedWriter. Here we do not need to pass a length because we have a single character.

import java.io.BufferedWriter;
import java.io.FileWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			FileWriter writer = new FileWriter("E:\\studytonight\\output.txt");  
			BufferedWriter buffer = new BufferedWriter(writer);  
			buffer.write(65);  
			buffer.close();  
			System.out.println("Data is written to the file successfully..."); 
		}
		catch(Exception e)
		{
			System.out.print(false);
		}
	}
}


Data is written to the file successfully...

output.txt

A

Conclusion

In this tutorial, we learned about write() method of BufferedWriter in Java. This method comes in the three overloading method, void write(char[] cbuf, int off, int len), void write(int c) and void write(String str, int off, int len) method.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.