Full Stack Web Development Internship Program
- 5k Enrolled Learners
- Weekend/Weekday
- Live Class
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:
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.
public class BufferedReader extends Reader
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. |
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();
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 |
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(); } } }
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
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.
Course Name | Date | |
---|---|---|
Java Certification Training Course | Class Starts on 28th January,2023 28th January SAT&SUN (Weekend Batch) | View Details |
Java Certification Training Course | Class Starts on 25th February,2023 25th February SAT&SUN (Weekend Batch) | View Details |
edureka.co