×

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

Exception Handling Program in Java

Exception Handling Program in Java Exception means something that is abnormal. In Java, an exception is treated as a problem that disrupts the normal flow of the program. An exception leads...

7 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Java Numbers

We use primitive data types like byte, int, long, double, etc to work with the numbers, When we need objects, we use wrapper class like Integer, Double, Long, Byte, etc....

2 minutes read.

Java Integer reverse() method

The reverse() method of Java Integer class returns the value obtained by reversing the order of the bits in the 2’s complement binary representation. Syntax public static int reverse (int i)  Parameters The parameter...

1 minute read.

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

How to take Multiple String Input in Java using Scanner class

Scanner class is a class which takes the multiple input data or the single input data through an objects and methods. The Scanner class will be available in the java.util...

3 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Java Binary to Hexadecimal

Converting between types in programming is an important task. Moving from one kind to another kind conversion is occasionally necessary. We have discussed numerous conversion types in the section on...

3 minutes read.

Java Regular Expressions

Java Regular Expressions The Java Regex or Regular Expression is an API that defines a pattern for searching or manipulating strings. A regular expression is a pattern that can be as simple as...

8 minutes read.

Java Print Writer

PrintWriter: It is used to write output data from the file. It is the class of Java.io package. It inherits the properties of the Writer class. Writer Class: It is a class of...

4 minutes read.

Java String length() method

Java String length() method returns length of the characters in a String. Syntax: public int length() Returns It returns length of the characters Java String length() method example 1          public class JavaStringLengthEx1  ...

1 minute read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

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.

Java Queue

The Java.util package has the interface Queue, which does extend the Collection interface. It is used to protect the parts that are managed using the FIFO approach. Being an interface, the...

5 minutes read.