How to write UTF-8 encoded data into a file – Java

Here’s the Java example to demonstrate how to write UTF-8 encoded data into a text file – “c:\\temp\\test.txt

P.S Symbol “??” is some “UTF-8” data in Chinese and Japanese

package com.mkyong;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

public class test {
	public static void main(String[] args){

	  try {
		File fileDir = new File("c:\\temp\\test.txt");
			
		Writer out = new BufferedWriter(new OutputStreamWriter(
			new FileOutputStream(fileDir), "UTF8"));

		out.append("Website UTF-8").append("\r\n");
		out.append("?? UTF-8").append("\r\n");
		out.append("??????? UTF-8").append("\r\n");
		
		out.flush();
		out.close();
	        
	    } 
	   catch (UnsupportedEncodingException e) 
	   {
		System.out.println(e.getMessage());
	   } 
	   catch (IOException e) 
	   {
		System.out.println(e.getMessage());
	    }
	   catch (Exception e)
	   {
		System.out.println(e.getMessage());
	   } 
	}	
}

Result

utf8 result
author image

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities. Read all published posts by

Comments

avatar
newest oldest most voted
suthershan
Guest
suthershan

Thank you so much, It works fine for me… Its very helpful.

package com.sprs2.action;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class TestData {
public static void main(String[] args) {

try {
File fileDir = new File(
“C:\\Migration\\DateFormatConvert\\tamil.html”);
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream(fileDir), “UTF8”));
String str;

File outFileDir = new File(“C:\\Migration\\DateFormatConvert\\tamil_out.html”);
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFileDir), “UTF8”));

while ((str = in.readLine()) != null) {
System.out.println(str);
out.append(str).append(“\r\n”);
}

in.close();

out.flush();
out.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}

trackback
How to read UTF-8 encoded data from a file - Java | Java

[…] P.S File is created by this article How to write UTF-8 encoded data into a file – Java […]

ganba
Guest
ganba

Hello there, This code does not really work for me. Here’s how I tested it. My test.txt file is saved with UTF-8 encoding and contains this line: ————— w été jedn? stron? ôpèç Ûg ütà ————— My test program below first reads the file in BufferedReader and then writes in Writer. ————— package test; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; public class Test_temp { public Test_temp() { String sPath = “E:/workspace/project/src/test/test.txt”; if (sPath != null && !sPath.trim().equals(“”)) { try { Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(sPath +… Read more »

Tony
Guest
Tony

ganba

are you sure that you’re using a UTF-( reader to validate the file?

Regards,

Tony

dongbangws
Guest
dongbangws

String UTF8Str = new String(s.getBytes(), “UTF8?);

ShouldBE? String UTF8Str = new String(s.getBytes(“UTF8?));

nitin
Guest
nitin

Code ain’t working for me may be because of this : http://stackoverflow.com/a/4053854

Please provide another working solution asap. I guess there is something in Apache Commons for the same.

Palmer Eldritch
Guest
Palmer Eldritch

You should close the streams in Finally blocks !!! The correct code is :

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;

public class Test { // capital for the class name

public static void main(String[] args) throws FileNotFoundException {
File fileDir = new File(“c:\\temp\\test.txt”);
final FileOutputStream file = new FileOutputStream(fileDir);
try {
Writer out = new BufferedWriter(
new OutputStreamWriter(file, “UTF8”));
out.append(“Website UTF-8”).append(“\r\n”);
out.append(“?? UTF-8”).append(“\r\n”);
out.append(“??????? UTF-8”).append(“\r\n”);
out.flush(); // if we called out.close() this would not be needed
} catch (UnsupportedEncodingException e) {
System.out.println(e.getMessage());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
file.close(); // we close the stream – the decorators
// need not be closed
} catch (IOException e) {
// you must log this
e.printStackTrace();
}
}
}
}

Rahulsingh
Guest
Rahulsingh

awesome !

Pranava
Guest
Pranava

How to create a file with chinese characters in the file name

cyber
Guest
cyber

I had this problem, but simply switching from Gedit to VSCode worked for me

BOB
Guest
BOB

how to use scanner input in java to convert unicode code point to utf8, utf16, utf32

Kauhsick
Guest
Kauhsick

Helped me a lot thanks