How to write a UTF-8 file with Java?

JavaFile IoUtf 8

Java Problem Overview


I have some current code and the problem is its creating a 1252 codepage file, i want to force it to create a UTF-8 file

Can anyone help me with this code, as i say it currently works... but i need to force the save on utf.. can i pass a parameter or something??

this is what i have, any help really appreciated

var out = new java.io.FileWriter( new java.io.File( path )),
        text = new java.lang.String( src || "" );
	out.write( text, 0, text.length() );
	out.flush();
	out.close();

Java Solutions


Solution 1 - Java

Instead of using FileWriter, create a FileOutputStream. You can then wrap this in an OutputStreamWriter, which allows you to pass an encoding in the constructor. Then you can write your data to that inside a try-with-resources Statement:

try (OutputStreamWriter writer =
             new OutputStreamWriter(new FileOutputStream(PROPERTIES_FILE), StandardCharsets.UTF_8))
    // do stuff
}

Solution 2 - Java

Try this

Writer out = new BufferedWriter(new OutputStreamWriter(
    new FileOutputStream("outfilename"), "UTF-8"));
try {
    out.write(aString);
} finally {
    out.close();
}

Solution 3 - Java

Try using FileUtils.write from Apache Commons.

You should be able to do something like:

File f = new File("output.txt"); 
FileUtils.writeStringToFile(f, document.outerHtml(), "UTF-8");

This will create the file if it does not exist.

Solution 4 - Java

Since Java 7 you can do the same with Files.newBufferedWriter a little more succinctly:

Path logFile = Paths.get("/tmp/example.txt");
try (BufferedWriter writer = Files.newBufferedWriter(logFile, StandardCharsets.UTF_8)) {
    writer.write("Hello World!");
    // ...
}

Solution 5 - Java

All of the answers given here wont work since java's UTF-8 writing is bugged.

http://tripoverit.blogspot.com/2007/04/javas-utf-8-and-unicode-writing-is.html

Solution 6 - Java

var out = new java.io.PrintWriter(new java.io.File(path), "UTF-8");
text = new java.lang.String( src || "" );
out.print(text);
out.flush();
out.close();

Solution 7 - Java

The Java 7 Files utility type is useful for working with files:

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import java.util.*;

public class WriteReadUtf8 {
  public static void main(String[] args) throws IOException {
    List<String> lines = Arrays.asList("These", "are", "lines");

    Path textFile = Paths.get("foo.txt");
    Files.write(textFile, lines, StandardCharsets.UTF_8);

    List<String> read = Files.readAllLines(textFile, StandardCharsets.UTF_8);

    System.out.println(lines.equals(read));
  }
}

The Java 8 version allows you to omit the Charset argument - the methods default to UTF-8.

Solution 8 - Java

we can write the UTF-8 encoded file with java using use PrintWriter to write UTF-8 encoded xml

Or Click [here](http://dharmeshpatel.ca/?p=46 "How to write a UTF-8 file with Java?")

PrintWriter out1 = new PrintWriter(new File("C:\\abc.xml"), "UTF-8");

Solution 9 - Java

Below sample code can read file line by line and write new file in UTF-8 format. Also, i am explicitly specifying Cp1252 encoding.

	public static void main(String args[]) throws IOException {

	BufferedReader br = new BufferedReader(new InputStreamReader(
			new FileInputStream("c:\\filenonUTF.txt"),
			"Cp1252"));
	String line;

	Writer out = new BufferedWriter(
			new OutputStreamWriter(new FileOutputStream(
					"c:\\fileUTF.txt"), "UTF-8"));

	try {

		while ((line = br.readLine()) != null) {

			out.write(line);
			out.write("\n");
			
		}

	} finally {

		br.close();
		out.close();

	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionmark smithView Question on Stackoverflow
Solution 1 - JavaskaffmanView Answer on Stackoverflow
Solution 2 - JavaMarkus LausbergView Answer on Stackoverflow
Solution 3 - JavaA_MView Answer on Stackoverflow
Solution 4 - JavaNigel_V_ThomasView Answer on Stackoverflow
Solution 5 - JavaEmperorlouView Answer on Stackoverflow
Solution 6 - JavaboxofratsView Answer on Stackoverflow
Solution 7 - JavaMcDowellView Answer on Stackoverflow
Solution 8 - JavaDharmesh PatelView Answer on Stackoverflow
Solution 9 - JavaAmmadView Answer on Stackoverflow