×

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

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

JDBC Program in Java

JDBC Program in Java JDBC is an API that defines how a client may access a database. It is a part of Java Standard Edition (Java SE). JDBC stands for Java...

4 minutes read.

Memory Areas in Java

Let’s have a look at how memory management in Java works. We will be going to discuss how the objects get destroyed, the working of a garbage collector, and things...

5 minutes read.

How to Convert String to Integer in Java

How to convert String to int in Java You need to convert String into int if you want to perform a mathematical operation on string which contains digits. To do so,...

3 minutes read.

Java Swing

Java Swing is the extension of Abstract Windows Toolkit (AWT). Any component designed in Swing will appear the same on any platform. Problems/Disadvantage of Abstract Windows Toolkit (AWT): As we know that...

27 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

How to compare dates in Java

Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if...

6 minutes read.

Static Array in Java

In this tutorial, we will study static arrays in Java. An array is a data structure that is of great importance in any programming language. It is classified into two...

3 minutes read.

Java Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

How to run Java Program in Eclipse

How to run Java Program in Eclipse In this section, we will learn how to write, save, compile, and execute or run a Java program in Eclipse. Eclipse is one of...

2 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

How to Reverse a String in Java

How to Reverse a String in Java There are a lot of ways to reverse a string in Java. One can use iteration, StringBuilder, StringBuffer to do the reverse of a...

6 minutes read.

How to Convert char to int in Java

How to Convert char to int in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

3 minutes read.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Sublime Number in Java

In this tutorial, we will understand what is meant by sublime number in java. From the point of the coding interview, it is one of the important topics. We will we will...

4 minutes read.

Java Integer remainder Unsigned() method

The remainderUnsigned() method of Java Integer class returns the unsigned remainder by dividing the first and second argument. Syntax public static int remainderUnsigned(int dividend, int divisor)  Parameters The ‘dividend’ and ‘divisor’ represents the value...

1 minute read.