Unlock a world of possibilities! Login now and discover the exclusive benefits awaiting you.
I'm trying to convert my generated text file to UTF with BOM, I tried in custom "UTF-BOM" and "UTF-8-BOM" but it is throwing unsupported encoding error... I tried this java code but it is writing empty file:
String filename = "/myTXTfilepath/file.txt";
String content="";
byte[] bytes = content.getBytes();
try (OutputStream out = new FileOutputStream(filename)) {
// write a byte sequence
out.write(0xEF);
out.write(0xBB);
out.write(0xBF);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
Hi @Moe ,
Instead of out.write can you use out.print() for UTF-8 BOM.
or instead of OutputStream please use PrintWriter class.
PrintWriter out = new PrintWriter(new File(filePath), "UTF-8");
out.write(csvFileContent);
Thanks and Regards,
Subhadip
Hello Moe....!
My original CSV files are encoded with UTF8-BOM, this is so Excel can recognize the Unicode characters (If its just encoded with UTF8 then Excel does not recognize the Unicode characters).
My issue is that with a UTF8 encoded CSV file, when you click save in Excel it gives you a popup option to keep the original formatting. However if the file is UTF8-BOM encoded then clicking the save is instant and there is no option to keep the formatting. The CSV file then has it commas replaced with tabs.
Thanks, I tried PrintWriter, FileWriter with BufferedReader but of them created a file with UTF8 encoding and not utf8 with BOM
This method will work.....
Awesome it usefull to make strong Article. Thank you writer.