Reading and Writing UTF-8 Data into File

Many times we need to deal with the UTF-8 encoded file in our application. This may be due to localization needs or simply processing user input out of some requirements.

Even some data sources may provide data in UTF-8 format only. In this Java tutorial, we will learn two very simple examples of reading and writing UTF-8 content from a file.

1. Writing UTF-8 Encoded Data into a File

The given below is a Java example to demonstrate how to write “UTF-8” encoded data into a file. It uses the character encoding “UTF-8” while creating the OutputStreamWriter.

File file = new File("c:\\temp\\test.txt");

try (Writer out = new BufferedWriter(new OutputStreamWriter(
  new FileOutputStream(file), StandardCharsets.UTF_8))) {

  out.append("Howtodoinjava.com")
    .append("\r\n")
    .append("UTF-8 Demo")
    .append("\r\n")
    .append("क्षेत्रफल = लंबाई * चौड़ाई")
    .append("\r\n");

  out.flush();
} catch (Exception e) {
  System.out.println(e.getMessage());
}

We need to enable the Eclipse IDE for support of the UTF-8 character set before running the example in Eclipse. By default, it is disabled. If you wish to enable the UTF-8 support in eclipse, we will get the necessary help for my previous post:

Read: How to compile and run a java program written in another language

2. Reading UTF-8 Encoded Data from a File

We need to pass StandardCharsets.UTF_8 into the InputStreamReader constructor to read data from a UTF-8 encoded file.

File file = new File("c:\\temp\\test.txt");

try (BufferedReader in = new BufferedReader(
  new InputStreamReader(new FileInputStream(file), "UTF8"))) {
  String str;

  while ((str = in.readLine()) != null) {
    System.out.println(str);
  }

} catch (Exception e) {
  System.out.println(e.getMessage());
}

Program Output:

Howtodoinjava.com
UTF-8 Demo
क्षेत्रफल = लंबाई * चौड़ाई

Happy Learning !!

Source Code on Github

Was this post helpful?

Join 7000+ Awesome Developers

Get the latest updates from industry, awesome resources, blog updates and much more.

* We do not spam !!

4 thoughts on “Reading and Writing UTF-8 Data into File”

  1. instead of giving output क्षेत्रफल = लंबाई * चौड़ाई it is giving ????=???? * ???. how to overcome this error?

    Reply
  2. Hi Lokesh,

    My question is somewhat different from this post.
    How to escape accented characters(it may include UTF-8, UTF-16…) apart from using apache commans.lang library.

    Thanks in advance.

    Regards,

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and related technologies, the best practices, algorithms, and interview questions.