×

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 vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

How to sort a String in Java

Sorting is the process of putting the elements in a certain order, either ascending or descending. Mostly the alphabetical order or natural order is used for a string. In other...

5 minutes read.

&& Operator in Java

“ && ” is the conditional - And operator in Java. In Java, it is an example of a logical operator. In Java, the “ & ” operator has two...

3 minutes read.

String Concatenation in Java

In Java, it gathers a new String that combines several strings. Following are the manners to concatenate strings in Java: By + (String concatenation) operatorBy concat() method By + (String concatenation) operator Java...

4 minutes read.

Convert Char array to string in java

A collection of characters is referred to as a string. A character array differs from a string in that the string is canceled by the special character "\0." A string...

4 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.

Sorting Program in Java

Sorting Program in Java: The sorting program in Java is used to sort arrays either in ascending or descending order.  There are two predefined methods available in Java that are...

6 minutes read.

Java Open File

Java Desktop class give an open() strategy to open a filr. It has a place with a java.awt package. Work area execution is stage subordinate, so it is important to...

5 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

Banking Application in Java

JDBC (Java Database Connectivity), which provides an API to connect to, execute, and fetch data from any databases, can be used to handle transactions in Java. There are several factors...

7 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

Java Solid Principles

Java implements the object-oriented SOLID principles for the design of software architecture. Solid Principles Java implements the object-oriented SOLID principles for the design of software architecture.   Five guiding principles transformed...

4 minutes read.

Method chaining in Java

Method chaining in Java is the act of calling a series of methods one after the other. The sole distinction between it and function Object () constructor chaining is the difference...

3 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.