×

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value.

Program:

Import java.io.*;
class  A
{
  public static void main(String s[])
 {
    int var1=1;
// Here “var1” assigned to 1.
  System. out. print ln (“var1 = “+var1);
}
}

Input:

1

Output:

var1=1

Note: The above program is about considering static values of variables in programming.

To access input from the user or to access dynamic values of variables from the keyboard, we use the Scanner class or BufferReader class.

BufferedReader:

  • It is a class that reads data from the input stream.
  • This class inherits the properties of the Reader class.
  • It takes as a string while reading the inputs.

    Input Stream:
    • Java.io package contains Input Stream class.
    • This stream represents the set of bytes.
    • It is an abstract class.

Abstract Class: Abstract methods are present in this class.An abstract method has an abstract keyword, and these methods don’t have anybody.

The syntax for object creation In Input Stream:

InputStream object1 = new FileInputStream();

Note: To create an object, we use FileInputStream because InputStream is an abstract class.

BufferedReader class Syntax:

public class BufferedReader extends Reader;

Constructors in BufferedReader:

  • BufferedReader(Reader r)
  • BufferedReader(Reader r, int size)

BufferedReader(Reader r): It is used to generate an Input stream without mentioning or considering the size.

BufferedReader(Reader r, int size): It is used to generate an Input stream with mentioned size.

Methods in BufferedReader class:

  • read ()
  • close ()
  • read (char[] c, int start, int length)
  • skip (long s)
  • ready ()
  • mark ()
  • reset ()
  • mark supported ()

read():

  • It is used to read single characters from the stream.
  • It returns -1 if it reaches the end of the input.
  • It represents whether the next character is present or not.
  • The return type of the read() method is int(Integer).

Syntax:

public int read()

Program:

import java.io.*;
class Example1 
{
 public static void main(String[] s) throws IOException 
{
BufferedReader object2 = new BufferedReader(new InputStreamReader(System.in));
char s1 = (char)object2.read();
System.out.println(s1);
}
}

Input:

A

Output:

A

readLine ():

  • It is used to read the entire line of the input.
  • It takes input in the form of strings.

Program:

import java.io.*;
class Example19 
{
public static void main(String[] s) throws IOException 
{
BufferedReader object2 = new BufferedReader(new InputStreamReader(System.in));
String string1 = object2.readLine();
System.out.println(string1);
}
}

 Input:

Java

Output:

Java

close ():

  • It is used to close the character stream.
  • Return type of the close() method is void.
  • If the stream has been closed, performing methods like read() and other methods throw an IOException.

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

  • It stores the reading characters in the array form.
  • It returns the count of characters.
  • Integer is the return type.

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.*;
class Example3{
public static void main(String[] s) throws IOException {
ob1 = new FileInputStream("cse.txt");
BufferedReader object2 = new BufferedReader(new InputStreamReader(ob1));
char c[]=new char[10];
try{
object2.read(c,0,4);
System . out .  println(c);
 object2.close()
}
catch (IOException E){
System. out. println(E);
}
}

Output: 

 ABCD

Note: Here In file cse.txt contains input as ABCDEFGHIJKLMNOPQRSTUVWXYZ.

skip(long s):

  • It returns the number of skipped characters from the stream.
  • Long is the return type.

Syntax:

public long skip(long s)

Program:

import java.io.*;
class Example14
{
public static void main(String s[]) throws IOException
{
Object1 = new FileInputStream(“cse-c.txt”);
BufferedReader b = new BufferedReader(new InputStreamReader(object1));
System . out . print((char)b.read());
b.skip(3);
  System . out . print((char)b.read());
 }  
}

Output:

H
o

Note: In the cse file we have Hello text we take this as input.

ready():

  • It checks whether the input stream is ready to be read or not.
  • Return type of ready() method is boolean.

Syntax:

public boolean ready();

Program:

import java.io.*;
    class Example18
    {
      public static void main ( String s[]) throws IOException
     {
object1 = new FileInputStream(“cse-c.txt”);
BufferedReader b = new BufferedReader(new InputStreamReader(object1));   System . out . println(“Print true if it is ready otherwise false:”+string2.ready()); b.close();}
}

Output:

Print true if it is ready otherwise false: true

mark(int Limit):

  • This method is used to represent the present position in the stream.
  • Void is the return type.

Syntax:

public void mark(int Limit)

Program:

import java.io.*;  
class Example17{  
public static void main(String[] s) throws IOException{  
object1 = new FileInputStream(“Fruit.txt”);
BufferedReader b = new BufferedReader(new InputStreamReader(object1));
 b . mark(1);  System . out . println(b.read());
b . close();  
  }  
} 

Output:

65

Note: In the fruits file we have content as Apple.

reset ():

  • It is used to reset the stream.
  • Void is the return type.

Program:

import java.io.*;  
class Example10 
{  
public static void main(String[] s) throws IOException
{  
object1 = new FileInputStream(“computerscience.txt”);
BufferedReader b = new BufferedReader(new InputStreamReader(object1));
b.mark(1);  
System . out . println(b . read());
b . reset();
b . close();  
} 
}

Output:

67

Note: In the computerscience file we have content like Coding.

mark supported ():

  • It is used to check whether it supports the mark() method or not.
  • void is the return type.

Syntax:

public void markSupported()

Program:

import java.io.*;
class Example7
{
public static void main(String[] s) throws IOException
{
try {
object1 = new FileInputStream(“computerscience.txt”);
BufferedReader b = new BufferedReader(new InputStreamReader(object1));
System . out . println(“Print True if it supports otherwise False:"+ b.markSupported());
b.close();
}
}
}

Output :

Print True if it supports otherwise False: true

Related Topics

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

Java Implements Keyword

To understand about the keyword implements we need to learn about the concept of the interfaces and inheritance in java programming languages. So let us learn about the interface and...

3 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

How to add 6 Months to the Current Date in Java?

In this tutorial, we will learn how to add 6 months to the local or current date in Java language. We will begin our topic with basic concepts and would...

3 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes 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.

Local Minima in Java

An Array Finding a local minimum in an array a[0. m-1] of different integers is the job. A[i] is considered a local minimum if it is smaller than two of its...

4 minutes read.

Iterate JSON array in Java

This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array. Json Array Ordered lists of...

4 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Java Program to Sort an Array of 0's, 1's, and 2’s | Dutch National Flag Problem in Java

The famed Dutch computer programmer Edsger Dijkstra's Dutch Nation Flag (DNF) challenge ranks among the most well-known programming challenges. The Dutch tricolor flag, comprising red, white, and blue, is the...

3 minutes read.

Sparse Numbers in Java

In this section, we will be very well acknowledged about the sparse numbers in Java, how a number can be verified if it is a sparse number or not. Sparse Numbers Any...

3 minutes read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

How to Update Java

As we all know that java can be installed in all operating systems like windows, Linux, macOS. We are available with the java 17 and java 18 versions in the...

3 minutes read.

Set Matrix Zeros in Java

In coding round interviews, it is frequently asked as the most significant challenge. an m*n matrix is provided. Set the entire column and row of the matrix to 0 if any...

6 minutes read.

Java TreeMap

TreeMap in Java with Example Java TreeMap implements the NavigableMap interface. It extends Map Interface. Java TreeMap is based on the red-black Tree implementation. It stores the key-value pair in sorted...

7 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 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.

Upcasting in Java

To know what is Upcasting in Java we should first equip ourselves about what is casting or type casting in Java. Casting The process of providing or replacing a reference variable of...

3 minutes read.