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.
[toc]
Using Files’s newBufferedReader()
We can use java.nio.file.Files's newBufferedReader() to read UTF8 data to String.
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.txtwas written from this example.
Using BufferedReader
We need to pass encoding as UTF8 while creating new InputStreamReader.
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.txtwas written from this example.
Using DataInputStream’s readUTF() method
We can use DataInputStream readUTF() to read UTF8 data to file.
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.txtwas written from this example.
That’s all about how to write UTF-8 Encoded Data in java