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 FileReader - writing text to files in Java with FileReader Java FileReaderlast modified March 31, 2024
In this article we shows how to use The examples use this text file: 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.
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. Always specify the encoding
when using FileReader.
The read method
In the following example, we use Main.java
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
void main() throws IOException {
var fileName = "thermopylae.txt";
char[] buf = new char[1024];
try (var fr = new FileReader(fileName, StandardCharsets.UTF_8)) {
int c;
while ((c = fr.read(buf)) != -1) {
System.out.println(buf);
}
}
}
The example reads a text file. It uses a character buffer to improve the performance of the operation. try (var fr = new FileReader(fileName, StandardCharsets.UTF_8)) {
The first argument of the int c;
while ((c = fr.read(buf)) != -1) {
System.out.println(buf);
}
The Reading with BufferedReader
Main.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
void main() throws IOException {
var fileName = "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 wrap the String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
The Count word frequencyIn the following example, we count the frequency of words in the text file. Main.java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.nio.charset.StandardCharsets;
void main() throws IOException {
var fileName = "thermopylae.txt";
var wordFreq = new HashMap<String, Integer>();
try (var bufferedReader = new BufferedReader(new FileReader(fileName,
StandardCharsets.UTF_8))) {
String line;
while ((line = bufferedReader.readLine()) != null) {
var words = line.split("\s");
for (var word : words) {
if (word.endsWith(",") || word.endsWith("?")
|| word.endsWith("!") || word.endsWith(".")) {
word = word.substring(0, word.length()-1);
}
if (wordFreq.containsKey(word)) {
wordFreq.put(word, wordFreq.get(word) + 1);
} else {
wordFreq.put(word, 1);
}
}
}
wordFreq.forEach((k, v) -> {
System.out.printf("%s -> %d%n", k, v);
});
}
}
We read the file line by line and split the lines into words. Then we remove any possible commands, question marks, exclamation marks, or dots from the words. The frequencies of words are kept in a hashmap. SourceJava FileReader - language reference
In this article we have presented the Java 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. |