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 in Java.
Table of Contents
Using Files’s newBufferedReader()
We can use java.nio.file.Files's newBufferedReader()
to read UTF8 data to String.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
package org.arpit.java2blog; import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; public class ReadUTF8NewBufferReaderMain { public static void main(String[] args) { readUTF8UsingnewBufferReader(); } // using newBufferedReader method of java.nio.file.Files public static void readUTF8UsingnewBufferReader() { Path path = FileSystems.getDefault().getPath("/users/apple/WriteUTF8newBufferWriter.txt"); Charset charset = Charset.forName("UTF-8"); try { BufferedReader read = Files.newBufferedReader(path, charset); String line = null; while ((line = read.readLine()) != null) { System.out.println(line); } read.close(); } catch (IOException e) { e.printStackTrace(); } } } |
Output:
Please note that
WriteUTF8newBufferWriter.txt
was written from this example.
Using BufferedReader
We need to pass encoding as UTF8
while creating new InputStreamReader
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
package org.arpit.java2blog; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; public class ReadUTF8DataMain { public static void main(String[] args) { try { File utf8FilePath = new File("/users/apple/UTFDemo.txt"); BufferedReader reader = new BufferedReader( new InputStreamReader(new FileInputStream(utf8FilePath), "UTF8")); String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); } } } |
Output:
यह हिंदी का वाक्य है
Please note that
UTFDemo.txt
was written from this example.
Using DataInputStream’s readUTF() method
We can use DataInputStream readUTF()
to read UTF8 data to file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.arpit.java2blog; import java.io.DataInputStream; import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; public class ReadUTFDataMain { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("/users/apple/WriteUTFDemo.txt"); DataInputStream dis = new DataInputStream(fis); String utf8FileData = dis.readUTF(); System.out.println(utf8FileData); dis.close(); } catch (EOFException ex) { System.out.println(ex.toString()); } catch (IOException ex) { System.out.println(ex.toString()); } } } |
Please note that
WriteUTFDemo.txt
was written from this example.
That’s all about how to write UTF-8 Encoded Data in java