Read a file character by character/UTF8: Difference between revisions
Read a file character by character/UTF8 (view source)
Revision as of 10:31, 2 February 2024
, 4 months ago→{{header|Wren}}: Changed to Wren S/H
m (→{{header|Wren}}: Changed to Wren S/H) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 370:
=={{header|Java}}==
The ''FileReader'' class offers a ''read'' method which will return the integer value of each character, upon each call.<br />
When the end of the stream is reached, -1 is returned.<br />
You can implement this task by enclosing a ''FileReader'' within a class, and generating a new character via a method return.
<syntaxhighlight lang="java">▼
import java.io.FileReader;▼
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class Program {
▲<syntaxhighlight>
private final FileReader reader;
public Program(String path) throws IOException {
▲import java.io.FileReader;
reader = new FileReader(path, StandardCharsets.UTF_16);
}
/** @return integer value from 0 to 0xffff, or -1 for EOS */
public int nextCharacter() throws IOException {
return reader.read();
}
public void close() throws IOException {
reader.close();
}
}
</syntaxhighlight>
===Using Java 11===
<syntaxhighlight lang="java">
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public final class ReadFileByCharacter {
public static void main(String[] aArgs) {
try (
int value;
while ( ( value =
System.out.println((char) value);
}
Line 1,417 ⟶ 1,444:
=={{header|Wren}}==
<syntaxhighlight lang="
File.open("input.txt") { |file|
|