Writing UTF8 data to a file using Java


In general, data is stored in a computer in the form of bits (1 or, 0). There are various coding schemes available specifying the set of bytes represented by each character.

Unicode (UTF) − Stands for Unicode Translation Format. It is developed by The Unicode Consortium. if you want to create documents that use characters from multiple character sets, you will be able to do so using the single Unicode character encodings. It provides 3 types of encodings.

  • UTF-8 − It comes in 8-bit units (bytes), a character in UTF8 can be from 1 to 4 bytes long, making UTF8 variable width.

  • UTF-16 − It comes in 16-bit units (shorts), it can be 1 or 2 shorts long, making UTF16 variable width.

  • UTF-32 − It comes in 32-bit units (longs). It is a fixed-width format and is always 1 "long" in length.

Writing UTF data to a file

The write UTF() method of the java.io.DataOutputStream class accepts a String value as a parameter and writes it in using modified UTF-8 encoding, to the current output stream. Therefore to write UTF-8 data to a file −

  • Instantiate the FileOutputStream class by passing a String value representing the path of the required file, as a parameter.

  • Instantiate the DataOutputStream class bypassing the above created FileOutputStream object as a parameter.

  • Write UTF data to the above created OutputStream object using the write UTF() method.

  • Flush the contents of the OutputStream object to the file (destination) using the flush() method

Example

 Live Demo

import java.io.DataOutputStream;
import java.io.FileOutputStream;
public class UTF8Example {
   public static void main(String args[]) throws Exception{
      //Instantiating the FileOutputStream class
      FileOutputStream fileOut = new FileOutputStream("D:\samplefile.txt");
      //Instantiating the DataOutputStream class
      DataOutputStream outputStream = new DataOutputStream(fileOut);
      //Writing UTF data to the output stream
      outputStream.writeUTF("టుటోరియల్స్ పాయింట్ కి స్వాగతిం");
      outputStream.flush();
      System.out.println("Data entered into the file");
   }
}

Output

Data entered into the file

The newBufferedWriter() method of the java.nio.file.Files class accepts an object of the class Path representing the path of the file and an object of the class Charset representing the type of the character sequences that are to be read() and, returns a BufferedWriter object that could write the data in the specified format

The value for the Charset could be StandardCharsets.UTF_8 or, StandardCharsets.UTF_16LE or, StandardCharsets.UTF_16BE or, StandardCharsets.UTF_16 or, StandardCharsets.US_ASCII or, StandardCharsets.ISO_8859_1

Therefore to write UTF-8 data to a file −

  • Create/get an object of the Path class representing the required path using the get() method of the java.nio.file.Paths class.

  • Create/get a BufferedWriter object, that could write UtF-8 data, bypassing the above-created Path object and StandardCharsets.UTF_8 as parameters.

  • Append the UTF-8 data to the above created BufferedWriter object using the append().

  • Flush the contents of the BufferedWriter to the (destination) file using the flush() method.

Example

 Live Demo

import java.io.BufferedWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class UTF8Example {
   public static void main(String args[]) throws Exception{
      //Getting the Path object
      Path path = Paths.get("D:\samplefile.txt");
      //Creating a BufferedWriter object
      BufferedWriter writer = Files.newBufferedWriter(path, StandardCharsets.UTF_8);
      //Appending the UTF-8 String to the file
      writer.append("టుటోరియల్స్ పాయింట్ కి స్వాగతిం");
      //Flushing data to the file
      writer.flush();
      System.out.println("Data entered into the file");
   }
}

Output

Data entered into the file

Updated on: 10-Sep-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements