GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing zetcode.com — Java BufferedReader - using Java BufferedReader to improve reading performance Java BufferedReaderlast modified January 27, 2024 Java BufferedReader tutorial shows how to use Java BufferedReader to improve reading performance of text files. Java tutorial is a comprehensive tutorial on Java language. Buffered input streams read data from a memory area known as a buffer. The native input API is called only when the buffer is empty. For unbuffered I/O stream, each read request is handled directly by the underlying OS. This is much less efficient, since each such request often triggers disk access, network activity, or some other operation that is relatively expensive. Java BufferedReader
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. Some of the following examples use this text file: src/resources/thermopylae.txt
The Battle of Thermopylae was fought between an alliance of Greek city-states, led by King Leonidas of Sparta, and the Persian Empire of Xerxes I over the course of three days, during the second Persian invasion of Greece. Java BufferedReader example
In the following example, we use
Note: In the past,
FileReader relied on the
default platform's encoding. Since Java 11, the issue was corrected. It is
possible now to explicitly specify the encoding.
com/zetcode/BufferedReaderEx.java
package com.zetcode;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class BufferedReaderEx {
public static void main(String[] args) throws IOException {
var fileName = "src/resources/thermopylae.txt";
try (var br = new BufferedReader(new FileReader(fileName, StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
}
In the example, we use the var fileName = "src/resources/thermopylae.txt"; We specify the file name. try (var br = new BufferedReader(new FileReader(fileName, StandardCharsets.UTF_8))) {
We wrap the String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
The Java Files.newBufferedReader
The com/zetcode/BufferedReaderEx2.java
package com.zetcode;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class BufferedReaderEx2 {
public static void main(String[] args) throws IOException {
var fileName = "src/resources/thermopylae.txt";
var path = Paths.get(fileName);
try (var br = Files.newBufferedReader(path, StandardCharsets.UTF_8)) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
}
The example reads the Java BufferedReader with InputStreamReader
The following example uses com/zetcode/BufferedReaderEx3.java
package com.zetcode;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class BufferedReaderEx3 {
public static void main(String[] args) throws IOException {
var urlPath = "http://webcode.me";
var url = new URL(urlPath);
try (var br = new BufferedReader(new InputStreamReader(url.openStream(),
StandardCharsets.UTF_8))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
}
The example reads the home page of the SourceJava BufferedReader - language reference In this article we have worked with Java BufferedReader. AuthorMy name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming. List all Java tutorials. |
| You are visitor 01402520 — 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. |