• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Java BufferedWriter Example

April 18, 2014 //  by Krishna Srinivasan//  Leave a Comment

BufferedWriter is useful in writing the sequence or stream of character to a file. The is fast compared to the FileOutputStream which is writing the stream of bytes. This class directly extends from the Writer class. It has two constructors. This class has the option to set the buffer size which is internally used by the BufferedWriter.

  • BufferedWriter(Writer out)- Creates a buffered character-output stream that uses a default-sized output buffer.
  • BufferedWriter(Writer out, int sz)- Creates a new buffered character-output stream that uses an output buffer of the given size.

Lets look at the example to understand how to use the BufferedWriter for writing into a file.

BufferedWriterExample.java

package javabeat.net.core;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Java BufferedWriter Example
 *
 * @author Krishna
 *
 */
public class BufferedWriterExample {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws IOException {

		//Create file instance
		File file = new File("NewTextFile.txt");

		//Create FileWriter instance
		FileWriter fileWriter = new FileWriter(file,false);

		//Create BufferedWriter instance
		BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

		//Write string to a file
		bufferedWriter.write("This is BufferedWriter Example!!");

		//Write newline character
		bufferedWriter.write("\n");

		//Write using Offset and length in the string
		bufferedWriter.write("Whole Text, Only Part is Written using Offset and Length",5,20);

		//Close the file writer object
		bufferedWriter.close();
	}

}

The new file would have created with the below content if you run the above example program.
NewTextFile.txt

This is BufferedWriter Example!!
 Text, Only Part is

Category: JavaTag: Java File IO

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Java StringReader Example
Next Post: Java PushbackReader Example »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact