×

Java Buffered Writer

BufferWriter Class:

It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is an abstract class.

Java Buffered Writer

Writer Class:

It is a class of Java.io package, it represents the set of characters. It is an abstract class.

Writer Subclasses:

  • Buffered Writer
  • File Writer
  • String Writer
  • Output Stream Writer

Writer class object creation Syntax:

Writer object1 = new FileWriter();

Note: Here, to create an object we use FileWriter because the Writer class is abstract.

BufferedWriter Class Declaration:

public class BufferedWriter extends Writer

Constructors in BufferedWriter:

  • BufferedWriter(Writer w)
  • BufferedWriter(Writer w, int s)

BufferedWriter(Writer w):

It generates an output stream without mentioning or considering the size.

Syntax:

BufferedWriter object1 = new BufferedWriter (w (or) file)

BufferedWriter(Writer w, int s): It generates an output stream by mentioning or considering the size.Here, s represents the size of the stream.

Syntax:

BufferedWriter object1 = new BufferedWriter( w (or) file , int s)

BufferedWriter Working Principle: When the write operation operates the characters are instead of writing into disk it writes on the internal Buffer then, a buffer is filled with characters then it writes on the disk

BufferedWriter class Methods:

  • newLine ()
  • write (int n)
  • write (char c[], int start, int length)
  • write (String s1, int start, int length)
  • flush ()
  • close ()

write (int n):

It is used for writing a single character to the internal buffer. The return type is a void, it overrides the write method which is present in the Writer class. It accepts only one parameter i.e, Integer type. It throws IOException which is an Input / Output Exception when an error occurs in Input or Output.

Syntax:

public void write(int n)

Program:

import java.io.*;
public class A
{
    public static void main(String[] args)
        throws IOException
 {
        StringWriter object1 = new StringWriter();
BufferedWriter  object2  = new BufferedWriter( object1);
object2.write(65);
object2.flush();
 System.out.println (object1.getBuffer());
    }
}

Output:

A

newLine ():

It includes a new line for the writer class. The return type is void. It does not return any value. It throws IOException i.e, Input / Output Exception when an error occurs in Input or Output.

Syntax:

public void newline()

Program:

import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException
    {
   StringWriter object1 = new StringWriter();
  BufferedWriter  object2 = new BufferedWriter(object1);
  object2.write(65);
  object2.newLine();
  object2.write(66);
  object2.flush();
  System.out.println (object1.getBuffer());
    }
}

Output:

A
B

write ( char c[], int start, int length):

 It writes the characters from a particular array to an internal buffer. It does not return any value When buffer size or length is equal to the size of the array then, it directly uses the main Stream.

It throws Input / Output Exception and Index out of bounds exception. I/O Exception occurs when errors are present in Input or Output, Index Out of bounds exception occurs when the negative length is passed through write method or length greater than an array.

Syntax:

public int read(char[] c, int start, int length)
  • c -It is the Destination buffer
  • start – at which index it starts writing characters.
  • length -.Several characters

Program:

import java.io.*;
public class Main{
    public static void main(String[] args) throws IOException
    {
   StringWriter object1 = new StringWriter();
  BufferedWriter  object2 = new BufferedWriter(object1);
  char c[] = [‘J’, ’A’, ’V’, ‘A’];
  object2 . write(c, 0, 2);
 object2 . newLine ();
 object2 . write(c,2,2);
  object2.flush();
  System.out.println (object1.getBuffer());
    }
}

OUTPUT:

JA
VA

write (String s1, int start, int length):

It is used to write the part of the string when it is passed through the Writer stream. It throws Input / Output Exception and Index out of bounds exception.

I/O Exception occurs when errors are present in Input or Output, Index Out of bounds exception occurs when the negative length is passed through write method or length greater than string.

Syntax:

public int read(String s1, int start, int length)

s1 -It is the Destination buffer

start – at which index it starts writing characters.

length -.Several characters

Program:

import java.io.*;
  class C{
    public static void main(String[] args) throws IOException
    {
   StringWriter object1 = new StringWriter();
  BufferedWriter  object2 = new BufferedWriter(object1);
  object2 . write(“JAVA”, 0, 2);
  object2.flush();
  System.out.println (object2.getBuffer());
}
}

Output:

JA

flush (): It is used to flush the characters and input stream. It does not return any value.

Syntax:

public void flush()

Program:

import java.io.*;
  class C{
    public static void main(String[] args) throws IOException
    {
   StringWriter object1 = new StringWriter();
  BufferedWriter  object2 = new BufferedWriter(object1);
  object2 . write(“JAVA”, 0, 2);
  object2.flush();
System.out.println(“Data is flushed”);
  System.out.println (object2.getBuffer());
}
}

Output:

Data is flushed.
JA

close (): It uses to close the Buffered Writer class.

Syntax:

public void close()

Related Topics

DatabaseMetaData in Java

The Data about another data is called Meta data. The DatabaseMetaData interface has the meta data of the database present in the system. It consists of database product name, total...

2 minutes read.

History and Evolution of Java

Java is invented by James Gosling, Patrick Naughton, Chris Warth, Ed Frank, and Mike Sheridan at Sun Microsystems, Inc. in 1991. Java is related to C++, which is inherited from...

3 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Program to Find the Common Elements between two Arrays in Java

In this article, we are going to learn how to find the common elements between two arrays using Java. Here, we use different approaches in Java to find the common...

3 minutes read.

Java Static Keyword

It can be either said that static declares the value to be the same, not only in the instance of a class but also as the whole. To declare a variable...

6 minutes read.

Java Integer doubleValue() method

The doubleValue() method of Integer class returns a double value for this Integer after a widening primitive conversion. Syntax public double doubleValue() Parameters NA Specified by This method is specified by doubleValue in class Number Return Value This...

1 minute read.

Activity selection problem in Java

The activity selection problem is a multiple objective problem hat requires choosing non-conflicting tasks to complete within a specific amount of time from a list of tasks identified by a...

5 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

Literals in Java

In this article we acknowledge ourselves about the term literals in java, types of literals and an example to each of them. Literal Literal refers to any constant value that can be...

4 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Three-way operator in Java

The ternary operator in Java is the only conditional operator that takes three operands. It's a popular one-line substitute for the if-then-else expression among Java programmers. If-else clauses can be...

3 minutes read.

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Convert JSON to Map in Java

JACKSON and GSON libraries are two extremely capable JSON-related libraries offered by Java. To efficiently work with the returned JSON data, we frequently need to convert JSON responses into a...

6 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.

Java Control Statements

Control Statements: Control statements in Java can also be referred to as decision-making while dealing with different problems. Control statements are helpful to sort out the flow of the program or...

7 minutes read.

Java Math pow() Method

The pow() method of Math class returns the value of first argument raised to the power of second argument i.e. ab. Syntax: public static double pow(double a, double b) Parameters: The parameters ‘a’ and...

3 minutes read.

Types of Logical Operators in Java

In this tutorial, we are going to study logical operators. A logical operator is an operator that accomplishes a logical operation that is to connect two or more operations. These...

5 minutes read.