GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - BufferedReader Class
Selected Reading
Java - BufferedReader ClassIntroductionThe Java BufferedReader class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.Following are the important points about BufferedReader −
Class declarationFollowing is the declaration for Java.io.BufferedReader class − public class BufferedReader extends Reader FieldFollowing are the fields for Java.io.BufferedReader class −
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Using mark() and reset() for Re-reading a Portion of the InputThe following example shows the usage of Java BufferedReader mark() and reset() methods. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) {
// Input string
String input = "Hello, World!\nThis is a BufferedReader example.\nHave a nice day!";
try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
// Read the first line
System.out.println(reader.readLine());
// Mark the current position in the stream
reader.mark(100); // Allows reading up to 100 characters before the mark is invalid
// Read the next line
System.out.println(reader.readLine());
// Reset the reader to the marked position
reader.reset();
// Re-read the line after reset
System.out.println(reader.readLine());
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Hello, World! This is a BufferedReader example. This is a BufferedReader example. Explanation
Example - Reading and Printing Each CharacterThe following example shows the usage of Java BufferedReader read() method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) {
String input = "Hello, World!";
// Initialize BufferedReader with a StringReader
try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
int character;
System.out.println("Reading characters one by one:");
// Read each character until the end of the stream
while ((character = reader.read()) != -1) {
// Print the character (cast the integer to char)
System.out.print((char) character);
}
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Reading characters one by one: Hello, World! Explanation
Example - Skipping Characters and Reading the Remaining InputThe following example shows the usage of BufferedReader skip(long n) method. BufferedReaderDemo.javapackage com.tutorialspoint;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
public class BufferedReaderDemo {
public static void main(String[] args) {
String input = "Hello, World! This is a BufferedReader skip() example.";
// Initialize BufferedReader with a StringReader
try (BufferedReader reader = new BufferedReader(new StringReader(input))) {
// Skip the first 7 characters
long skipped = reader.skip(7);
System.out.println("Skipped characters: " + skipped);
// Read and print the remaining input
String remaining = reader.readLine();
System.out.println("Remaining input: " + remaining);
} catch (IOException e) {
System.err.println("An error occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result − Skipped characters: 7 Remaining input: World! This is a BufferedReader skip() example. Explanation
Advertisements
| ||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |