logo

How to read text file in Java with user-defined charset

How to read text file in Java with charset (encoding) UTF-8 using InputStreamReader.

Which character set Character streams use? This depends on your operating system and it's locale. If you know in which encoding (charset) is your file, you can use InputStreamReader, in which you will define file encoding (charset).

try (BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream("file.txt"), "UTF-8"))) {
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}

comments powered by Disqus

rss

Free online Java web application examples

Want to develop whole web applications which use database, security and more? See my github account, where are lot's of example projects:
github

Search