×

Java BufferedWriter

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 Program to Add Digits Until the Number Becomes a Single Digit Number

We will develop Java programme that increase a number's digit count in order to reduce it to one. The issue is also known as the digit root problem. Example Consider the case where...

3 minutes read.

Salesman Problem in Java

The Traveling Salesman Problem determines the shortest path that visits each city approximately once and loops back to the starting location. Another Java problem that is most like the Traveling...

5 minutes read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Java Math sqrt() Method

The sqrt() method of Java Math class returns the accurately rounded positive square root of the specified double value. Syntax: public static double sqrt(double a) Parameters: The parameter ‘a’ represents the number whose square...

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.

Java Anon Proxy

The Java Anon Proxy (JAP), also known as JonDonym, is a proxy system designed to enable Web browsing with revocable (the use of or publication under a pseudonym, a false...

4 minutes read.

Java Socket Programming

Java Socket Programming Socket programming is used for the communication between the applications, i.e., client and server running on different JRE may be connection-oriented or connectionless. The client program can be designed using the...

8 minutes read.

Java Math log10() Method

The log10() method of Math class returns the base 10 logarithmic value for the specified double argument. Syntax: public static double log10(double a) Parameters: The parameter ‘a’ represents a double value. Return Value: The log10() method...

2 minutes read.

Java Integer parseInt() method

The parseInt() method of Java Integer class parses the CharSequence argument as a signed decimal integer. The second parameter parses the string argument as a signed integer in the radix specified...

2 minutes read.

Java File

Java file class implements the concept of file handling. It has several methods, such as deleting, creating, reading, and updating files. This class allows java users to perform various operations...

5 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

Split the Number String into Primes in Java

Given is a string that only contains digits and serves to represent a number. Our goal is to split the string of numbers in a way that ensures each segment...

2 minutes read.

Program to find the duplicate characters in a string

Problem statement You have given with a string and your task is to find out the repeated characters from the string and print them. If no character is repeated, then you...

2 minutes read.

Factorial of a Large Number in Java

We've already talked about a number's factorial. We must still go over the factorial of a large number separately, though. The method used to determine the factorial of a small...

8 minutes read.

Radix Sort in Java

Radix Sort in Java Radix sort in Java uses the digits of the numbers given in the sorted array to sort the numbers. The radix sort uses the place value of...

5 minutes read.

Java StringBuffer vs StringBuilder

Java StringBuffer vs StringBuilder StringBuffer The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences. Java StringBuffer program FileName: StringBufferExample.java // A basic program that demonstrates the working...

2 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.