• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Java PushbackReader Example

April 18, 2014 //  by Krishna Srinivasan//  Leave a Comment

The PushbackReader is useful when you parse data from a Reader. Sometimes you would have the requirement to read ahead a few characters to see what is coming next, before you can determine how to interprete the current character. The PushbackReader allows that facility for the programmers. This class defines two constructors.

  • PushbackReader(Reader in) – Creates a new pushback reader with a one-character pushback buffer.
  • PushbackReader(Reader in, int size) – Creates a new pushback reader with a pushback buffer of the given size.

Lets look at the example to understand how to use PushbackReader while reading a file.

PushBackReaderExample.java

[code lang=”java”] package javabeat.net.core;

import java.io.FileReader;
import java.io.IOException;
import java.io.PushbackReader;

/**
* Java StringReader Example
*
* @author Krishna
*
*/
public class PushBackReaderExample {

/**
* @param args
*/
public static void main(String[] args) throws IOException {
//Create Reader instance
FileReader reader = new FileReader("TextFile.txt");

//Create PushBackReader instance with setting 10 as unread
PushbackReader pushbackReader = new PushbackReader(reader,10);
int c = pushbackReader.read();
while (c != -1){
//Converting to character
System.out.print((char)c);
c= pushbackReader.read();
}
//Closing the file io
reader.close();

}

}

[/code]

TextFile.txt

[code] India
United Kingdom
Australia
Singapore
[/code]

Output…

[code] India
United Kingdom
Australia
Singapore
[/code]

Category: JavaTag: Java File IO

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « Java BufferedWriter Example
Next Post: JQuery bind() And unbind() Example jquery»

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact