×

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

Menu Driven Program in Java

Menu Driven Program in Java The menu-driven program in Java is a program that displays a menu and then takes input from the user to choose an option from the displayed...

3 minutes read.

Java Database Connectivity with MySQL

In this tutorial, we will learn how to connect Database with MySQL in Java. 5 Steps to Connect to the Database in Java Load the driver (or) Register the driver classEstablish a...

4 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

Pig Latin Program in Java

Pig Latin Program in Java Pig Latin is a method for translating words of the English language into a different language. It is an encrypted word that is generated by using the following steps....

4 minutes read.

Java Swing Time Picker

Prerequisites In this tutorial, we will learn the time picker in java swing. Before learn time picker, we should learn about java swing. Java Swing Introduction The Java Foundation Classes include Java Swing....

5 minutes read.

Sorting a String in Java

Sorting a String in Java Sorting is a process of arranging available data objects in a meaningful format that is ascending or descending order. Sorting a string or array of String...

4 minutes read.

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

Thread States in Java

An Item's Life Cycle (Thread States) A thread can be in any of the states mentioned above at any given time in Java. They are as follows: New Active I'm blocked or waiting Temporary...

3 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Hollow Diamond Pattern in Java

Why are patterns important? Programmers frequently create Java pattern programs to practice coding and ace interviews. Interviewers frequently test candidates' logical reasoning and implementation by asking about pattern programs. Hollow Diamond Pattern The...

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

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

SHA Decrypt in Java

In this section, we will be acknowledged about the decryption of SHA in Java. Java SHA The SHA cryptographic hash algorithm in cryptography outputs the hash value as an approximately 40-digit-long hexadecimal...

4 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Java Math abs() Method

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long. Syntax public static int abs(int a) public static float abs(float a) public...

2 minutes read.

Java OCR

In this article, you will be acknowledged about what is a tesseract OCR, how it works, what are its used and advantages and disadvantages. Also, you will be able to...

4 minutes read.