×

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

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

Shallow copy in Java

Java's most important task is making a copy or clone of an object. In this part, we'll talk about shallow copies in Java and how to make them of Java...

4 minutes read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

Bully Algorithm code in Java

Election algorithms include the bully algorithm, mainly used to select a coordinate. To find a coordinator in a distributed system that can carry out the tasks required by other processes,...

4 minutes read.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Narcissistic Number in Java

A Narcissistic number is made up of digits that have been added together and raised to powers equal to the number of digits in the original number. In those other...

3 minutes read.

Java Primitive Data Types

Primitive data types are the simplest data types in a programming language. They’re predefined in the language. The names of the primitive types are quite descriptive of the values that...

2 minutes read.

Java String lastIndexOf() method

Java String lastIndexOf() method returns last index of character or substring in a String. Syntax: Method Description int lastIndexOf(int ch)It returns last index position for the given char valueint lastIndexOf(int ch, int...

2 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

How to Iterate List in Java?

List is a Collection foundation interface in Java. It enables us to keep the collection of objects in order. The four classes that make up the List interface implementation are...

3 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

Hogben Numbers in Java

In this section, we will discover what the Hogben number is and develop Java programs that compute it. Java coding interviews and academic exams typically involve questions about the Hogben...

3 minutes read.

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Java Math rint() Method

The rint() method of Java Math class returns the double value which is close to the specified argument and is equal to mathematical integer. Syntax: public static double rint(double a) Parameters: The parameter ‘a’...

1 minute read.

Java Password Generator

Generally, we must create a strong password for security reasons. In Java, there are numerous strategies for creating secure passwords. We will learn how to create a strong password in...

4 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.