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

Line 371:
=={{header|Java}}==
 
<syntaxhighlight lang="java">
import static java.nio.charset.StandardCharsets.UTF_8;
 
import java.io.FileReader;
import java.io.IOException;
import static java.nio.charset.StandardCharsets.UTF_8;
 
public final class ReadFileByCharacter {
public static void main(String[] aArgs) {
String path = "C:/Users/psnow/Desktop/testFile.txt";
try ( FileReader readerfileReader = new FileReader(path, StandardCharsets.UTF_8); ) {
int value;
while ( ( value = fileReader.read() ) != END_OF_STREAM ) {
System.out.println((char) value);
}
} catch (IOException exceptionioe) {
ioe.printStackTrace();
}
}
private static final int END_OF_STREAM = -1;
 
public class FileUtil {
FileReader reader;
/** cast the returned value to 'char' */
int next() throws IOException {
int value;
try {
/* 'read' will return 0 through 0xffff, and -1 for EOS */
value = reader.read();
} catch (IOException exception) {
reader.close();
throw exception;
}
return value;
}
void open(String path) throws IOException {
reader = new FileReader(path, UTF_8);
}
void close() throws IOException {
reader.close();
}
}
</syntaxhighlight>
{{ out }}
<pre>
R
o
s
e
t
t
a
</pre>
 
=={{header|jq}}==
895

edits