Java Buffered Reader, How to Reset Back to the Top Line
BufferedReader in Java : How To Read Text From Input Stream
Last updated on Jun 10,2021 27.5K Views
6 / 12 Blog from Java Strings
Java provides several mechanisms in order to read from a file. One important class that helps in performing this operation is the BufferedReader. So, this article on BufferedReader in Java will help you in understanding Bufferedreader class along with examples. Following are the topics covered in this blog:
- What is BufferedReader in Java?
- BufferedReader Class Declaration
- Java BufferedReader Constructors
- Methods & Description
- Difference Between Scanner and BufferReader
- BufferedReader in JDK7 Example
- Reading Data From Console By InputStreamReader and BufferedReader
- Reading Data From Console Until User Writes Stop
What is BufferedReader in Java?
BufferedReader is a Java class that reads text from the input stream. It buffers the characters so that it can get the efficient reading of characters, arrays, etc. It inherits the reader class and makes the code efficient since we can read the data line-by-line with the readline() method. There are a few pointers we have to keep in mind while working with BufferedReader class in Java.
- We may have to specify the buffer size even though the default is large enough for any purpose.
- With each request made of a reader a corresponding, a read request is also made of an underlying character.
- It is always advised to wrap a BufferedReader class around any reader such as InputStreamReaders.
- For the programs that use DataInputaStreams for textual input, an appropriate BufferedReader replaces the DataInputStream to localize it.
BufferedReader Class Declaration
public class BufferedReader extends Reader
Java BufferedReader Constructors
| Constructor | Description |
| BufferedReader(Reader reader) | This constructor creates a buffering character-input stream that works on a default-size input buffer. |
| BufferedReader(Reader reader, int size) | It uses the specified size for the input buffer for buffering the character-input stream. |
Methods And Description
Following are the methods with the description that we have for the Java BufferedReader class.
| Method | Description |
| int read() | Reads a single character |
| String readLine() | It reads a line of text |
| void reset() | Repositions the stream to the position where the mark method was last called |
| int read(char[] cb, int off , int len) | Reads the characters in a portion of an array |
| boolean markSupported() | It tests the input stream support for reset and mark method |
| boolean ready() | It checks whether the input stream is ready for reading |
| long skip(long n) | skips the characters |
| void close() | It closes the input stream |
| void mark(int readAheadLimit) | Used to mark the current position in the stream |
Example:
import java.io.*; public class Example{ public static void main(String args[] throws Exception) { FileReader f = new FileReader("filelocation"); BufferedReader b = new BufferedReader(f); int i ; while((i = b.read()) != -1){ System.out.println((char) i); } b.close(); f.close(); Difference Between Scanner And BufferedReader
| BufferedReader | Scanner |
| Synchronous and should be used with multiple threads | Not synchronous and not used with multiple threads |
| Buffer memory is larger | Buffer memory is smaller |
| Faster than Scanner | Slower because it does parsing of the input data |
| There is no ambiguity related to nextline() method | There are a lot of problems with the nextline() method. |
| Uses buffering to read characters from the character-input stream | It is a simple text scanner which parses primitive types and strings |
BufferedReader in JDK7 Example
import java.io.*; public class Example{ public static void main(String[] args){ try( BufferedReader b = new BufferedReader(new fileReader("filename"))); { String s; while((s = b.readLine()) != null){ System.out.println(s); } } catch(IOException e) { e.printStackTrace(); } } } Reading Data From Console By InputStreamReader And BufferedReader in Java
import java.io.*; public class Example{ public static void main(String args[] throws Exception){ InputStreamReader i = new InputStreamReader(system.in); BufferedReader b = new BufferedReader(i); System.out.println("Enter Course"); String course = b.readLine(); System.out.pritln("Edureka" + course); } } Output:Enter Course Java Edureka Java
Reading Data From Console Until User Writes Stop
import java.io.*; public class Example{ public static void main(String args[] throws Exception){ InputStreamReader i = new InputStreamReader(system.in); BufferedReader b = new BufferedReader(i); string course = ""; while(!name.equals("stop")){ System.out.println("enter course:"); course = b.readLine(); System.out.println("Course is:" + course); } b.close(); i.close(); } } Output:enter course: Course is: Java enter course: Course is: stop
This brings us to the end of this article where we have learned how we can read characters from the character-input stream using the BufferedReader class in Java. Hope you are clear with all that has been shared with you in this tutorial.
If you found this article on "BufferedReader in Java" relevant, check out the Edureka's Java Certification Training, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe.
We are here to help you with every step on your journey and come up with a curriculum that is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.
If you come across any questions, feel free to ask all your questions in the comments section of "BufferedReader in Java" and our team will be glad to answer.
Recommended videos for you
Node JS : Steps to Create Restful Web App
Watch Now
Spring Framework : Introduction to Spring Web MVC & Spring with BigData
Watch Now
A Day In The Life Of A Node.js Developer
Watch Now
Learn Perl-the Jewel of Scripting Languages
Watch Now
Microsoft SharePoint-The Ultimate Enterprise Collaboration Platform
Watch Now
Building Web Application Using Spring Framework
Watch Now
Microsoft SharePoint 2013 : The Ultimate Enterprise Collaboration Platform
Watch Now
NodeJS – Communication and Round Robin Way
Watch Now
Building Application With Ruby On Rails Framework
Watch Now
Create Restful Web Application With Node.js Express
Watch Now
PHP and MySQL : Server Side Scripting For Web Development
Watch Now
PHP & MySQL : Server-side Scripting Language for Web Development
Watch Now
Effective Persistence Using ORM With Hibernate
Watch Now
Microsoft .NET Framework : An IntelliSense Way of Web Development
Watch Now
MS .Net – An IntelliSense Way of Web Development
Watch Now
Java Buffered Reader, How to Reset Back to the Top Line
Source: https://www.edureka.co/blog/bufferedreader-in-java/
0 Response to "Java Buffered Reader, How to Reset Back to the Top Line"
Post a Comment