Ad

How To Write To File In Java By Windows-1256 Encoding

- 1 answer

I am trying in JAVA 8, for "windows-1256" file Writing my last try was :

  String win1256="...";

//

  File file = new File ("C:\file1.txt");
  OutputStreamWriter   os = new OutputStreamWriter (new 
  FileOutputStream(file),"windows-1256");
  os.write(win1256);

or :

  FileOutputStream out = new FileOutputStream("C:\file1.txt");
  out.write(win1256.getBytes("windows-1256"));

however didn't work and output file was unreadable with "???..."

Ad

Answer

Changing is:

byte[] originalBytes; // Here the sequence of bytes representing the UTF-8 encoded string
Encoding enc = Encoding.GetEncoding("windows-1256");
byte[] newBytes = enc.GetBytes(Encoding.UTF8.GetString(originalBytes));
Ad
source: stackoverflow.com
Ad