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

Content deleted Content added
Reilas (talk | contribs)
Reilas (talk | contribs)
Line 378: Line 378:


public class FileUtil {
public class FileUtil {
FileReader reader;
String readFile(String path) throws IOException {
FileReader reader = new FileReader(path, UTF_8);
/** cast the returned value to 'char' */
StringBuilder string = new StringBuilder();
int next() throws IOException {
int value;
try {
try {
int value;
/* 'read' will return 0 through 0xffff, and -1 for EOS */
/* 'read' will return 0 through 0xffff, and -1 for EOS */
while ((value = reader.read()) != -1)
value = (char) reader.read();
string.append((char) value);
} catch (IOException exception) {
} finally {
reader.close();
reader.close();
throw exception;
}
}
return string.toString();
return value;
}
void open(String path) throws IOException {
reader = new FileReader(path, UTF_8);
}
void close() throws IOException {
reader.close();
}
}
}
}