How to Read UTF-8 Encoded Data in Java


In this example we will show the method to read UTF-8 encoded data from a file in Java.

Source Code

package com.beginner.examples;

import java.io.*;

public class FileOperator20Example { 
	public static void main(String[] args )
    {	
		String path = "example.txt";
		String line = "";
        try
        {
        
    		File file = new File(path);
    		InputStreamReader in = new InputStreamReader(new FileInputStream(file), "UTF8");
    		BufferedReader br = new BufferedReader(in);
    		while ((line = br.readLine()) != null) 
    		{
    			System.out.println(line);
    		}
    		in.close();
        } 
        catch (IOException e) 
        {
            e.printStackTrace();
        }
    }
}

Output:

1
2
3
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments