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

Line 372:
 
<syntaxhighlight lang="java">
import static java.nio.charset.StandardCharsets.UTF_8;
 
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
 
public class Main {
 
public class MainFileUtil {
public static voidString mainreadFile(String[] argspath) throws IOException {
final int endOfFile = -1;
try ( FileReader reader = new FileReader("input.txt"path, StandardCharsets.UTF_8) ) {;
StringBuilder string = new StringBuilder();
try ( FileReader reader = new FileReader("input.txt", StandardCharsets.UTF_8) ) {
try {
while ( true ) {
int ch = reader.read() int value;
/* 'read' will return 0 through 0xffff, and -1 for EOS */
if ( ch == endOfFile ) {
while ((value = reader.read()) != -1)
break;
string.append((char) value);
}
} finally {
reader.close();
System.out.print(Character.toChars(ch));
}
}
return string.toString();
}
}
}
118

edits