×

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

HttpURLConnection

HttpURLConnection Protocol It is a standard set of rules that allow electronic devices to communicate with each other. The http protocol The http protocol is for data communication, distributing, collaborating, hypermedia information systems, on the World Wide...

5 minutes read.

How to add 4 Years to the Current Date in Java?

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

2 minutes read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

Modules in Golang

Modules are a way to manage dependency versions and enable reproducible builds of Go programs. They were introduced in Go 1.11 and are now the recommended way to manage dependencies...

4 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.

Basic Terms in Multithreading

To understand the terms of multithreading, we must have knowledge about concurrency, processes, and threads. Concurrency The concurrency stands for performing multiple tasks at the same time. In the process communication, the operating system permits the process...

8 minutes read.

Java float vs double

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

7 minutes read.

Thread Concept in Java

What is a Thread? A subprocess that is lightweight in Java is known as a thread. It is the smallest unit of a process. For the running of multiple tasks parallelly...

3 minutes read.

Brilliant Number in Java

It is a number N that is made up of two prime numbers that have the same number of digits and is called a brilliant number. Several/Some of the brilliant Numbers...

3 minutes read.

How to make Java Projects

Ant and Maven are both offered by NetBeans for the development of Java applications. When using Ant, the IDE creates an Ant build script depending on the settings you select...

6 minutes read.

Java Thread class

Thread class The thread represents a part of the process. Every process can have multiple associated threads in which every thread may execute the same or different job. By default, each thread assigns...

16 minutes read.

Read the XLSX file in Java

Excel files contain cells; reading an excel file in Java differs from reading a word file. JDK doesn't have a direct API that can read or write Word or Excel...

3 minutes read.

Unicode System in Java

In this tutorial, we will understand the meaning and emergence of the Unicode system in java. The Unicode system is one of the important and useful features in the world...

6 minutes read.

Constructor in Java with Example

Java Constructor  The constructor is used for object initialization. It's a block of code that initializes a newly created object. It contains a collection of statements that are executed at the...

5 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Java Get File Size

There are three ways to get the file size in Java. Using Java InputOutput Using NewInputOutput Library Files.size() Method FileChannel.size() Method Using Apache Commons InputOutput Using Java InputOutput: The Java IO package offers the classes that deal...

5 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

Java Boolean logicalXor() Method

The logicalXor() method of Java Boolean class returns the result of implementing logical XOR operation on the specified Boolean operands. Syntax: public static boolean logicalXor (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

JAR File in Java

What is a JAR File? JAR stands for Java Archive. It is a file format mainly used to combine many Java class files and the corresponding metadata and resources into one...

4 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.