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

Content deleted Content added
Reilas (talk | contribs)
PSNOW123 (talk | contribs)
Line 371: Line 371:
=={{header|Java}}==
=={{header|Java}}==


<syntaxhighlight lang="java">
<syntaxhighlight>
import static java.nio.charset.StandardCharsets.UTF_8;


import java.io.FileReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public final class ReadFileByCharacter {
public static void main(String[] aArgs) {
String path = "C:/Users/psnow/Desktop/testFile.txt";
try ( FileReader fileReader = new FileReader(path, StandardCharsets.UTF_8) ) {
int value;
while ( ( value = fileReader.read() ) != END_OF_STREAM ) {
System.out.println((char) value);
}
} catch (IOException ioe) {
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>
</syntaxhighlight>
{{ out }}
<pre>
R
o
s
e
t
t
a
</pre>


=={{header|jq}}==
=={{header|jq}}==