In this example we will show the method to write UTF-8 encoded data into a file in Java.
Source Code
package com.beginner.examples;
import java.io.*;
public class FileOperator21Example {
public static void main(String[] args )
{
try
{
File path = new File("example.txt");
if (!path.exists())
{
path.createNewFile();
}
FileOutputStream writerStream = new FileOutputStream(path);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(writerStream, "UTF-8"));
String content = "testrnwrite";
writer.write(content);
writer.close();
System.out.println("Done");
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Output:
Done