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

Comments

Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode