Write UTF-8 Encoded Data in java

In this post, we will see how to write UTF-8 Encoded Data.

Sometimes, we have to deal with UTF-8 Encoded Data in our application. It may be due localization or may be processing data from user input.

We will use Hindi language sentences to write in file.

There are three ways to write UTF-8 Encoded Data in Java.

Using Files’s newBufferWriter()

We can use java.nio.file.Files's newBufferedWriter() to write UTF8 data to file.

When you run above program, WriteUTF8newBufferWriter.txt will be created at /users/apple/WriteUTF8newBufferWriter.txt.
Let’s open the file and have a look at content.

WriteUTF-8NewBufferWriter

Using BufferedWriter

We need to pass encoding as UTF8 while creating new OutputStreamWriter.

When you run above program, UTFDemo.txt will be created at /users/apple/UTFDemo.txt.
Let’s open the file and have a look at content.

UTFDemo

Using DataOutputStream’s writeUTF() method

We can use DataOutputStream's writeUTF() to write UTF8 data to file.

Output:

WeiteUTFDemo

When you run above program, WriteUTFDemo.txt will be created at /users/apple/WriteUTFDemo.txt.
That’s all about how to write UTF-8 Encoded Data in java

Related Posts

  • 27 December

    How to get current working directory in java

    Learn about how to get current working directory in java using different ways

  • 08 August

    Difference between Scanner and BufferReader in java

    Table of ContentsIntroductionScannerBufferedReaderDifference between Scanner and BufferedReader In this post, we will see difference between Scanner and BufferReader in java. Java has two classes that have been used for reading files for a very long time. These two classes are Scanner and BufferedReader. In this post, we are going to find major differences and similarities […]

  • 29 March

    Read UTF-8 Encoded Data in java

    Table of ContentsUsing Files’s newBufferedReader()Using BufferedReaderUsing DataInputStream’s readUTF() method In this post, we will see how to read UTF-8 Encoded Data. Sometimes, we have to deal with UTF-8 Encoded Data in our application. It may be due localization or may be processing data from user input. There are multiple ways to read UTF-8 Encoded Data […]

  • 15 March

    Java read file line by line

    Table of ContentsJava 8 StreamsUsing BufferReaderUsing ScannerUsing FilesUsing RandomAccessFileUsing Apache Commons In this post, we will see different ways to read file line by line in java. Sometimes, we need to read file line by line to a String, and process it. Here are different ways to read file line by line in java. Java […]

  • 06 February

    Java FileWriter Example

    Table of ContentsConstructorsMethodsExampleCode:Output: The FileWriter class of java.io package makes writing to files in Java as easy as A-B-C. The FileWriter class writes to files as a stream of characters instead of bytes(as in FileOutputStream). Like its input-oriented counterpart FileReader, the FileWriter class is intended for writing “text” based files one character at a time. […]

  • 05 October

    Java FileReader Example

    Table of ContentsConstructorsMethodsExampleCode:Output: The FileReader class of java.io package provides a seamless and easy-to-use way to read and analyse the contents of a file using Java. The FileReader class, works similar to the FileInputStream class because it reads the file data as a stream. However, unlike the latter which reads files as a stream of […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.