Read a file character by character/UTF8: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(14 intermediate revisions by 4 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 />
<syntaxhighlight lang="java">import java.io.FileReader;
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.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
 
public class MainProgram {
private final FileReader reader;
 
public static void mainProgram(String[] argspath) throws IOException {
var reader = new FileReader("input.txt"path, StandardCharsets.UTF_8UTF_16);
while (true) {
int c = reader.read();
if (c == -1) break;
System.out.print(Character.toChars(c));
}
}
 
}</syntaxhighlight>
/** @return integer value from 0 to 0xffff, or -1 for EOS */
public int nextCharacter() throws IOException {
int c =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) {
Path path = Path.of("input.txt");
try ( BufferedReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8) ) {
int value;
while ( ( value = reader.read() ) != END_OF_STREAM ) {
System.out.println((char) value);
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
private static final int END_OF_STREAM = -1;
 
}
</syntaxhighlight>
{{ out }}
<pre>
R
o
s
e
t
t
a
</pre>
 
=={{header|jq}}==
Line 1,371 ⟶ 1,419:
<syntaxhighlight lang="tcl">fconfigure $channel -buffersize $byteCount</syntaxhighlight>
When the channel is only being accessed from Tcl (or via Tcl's C API) it is not normally necessary to adjust this option.
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">
import os
 
fn main() {
file := './file.txt'
mut content_arr := []u8{}
if os.is_file(file) == true {
content_arr << os.read_bytes(file) or {
println('Error: can not read')
exit(1)
}
}
else {
println('Error: can not find file')
exit(1)
}
 
println(content_arr.bytestr())
}
</syntaxhighlight>
 
=={{header|Wren}}==
<syntaxhighlight lang="ecmascriptwren">import "io" for File
 
File.open("input.txt") { |file|
9,482

edits