1

I am using the JDK 1.7 on Linux server. I have installed apache 6.0.35 and use the code

Writer w = new OutputStreamWriter(os,"Unicode");

to write a file in unicode format.

But the file is getting generated in unicode big endian format. How do I select a different output format?

Lennart
  • 9,657
  • 16
  • 68
  • 84
prasad
  • 11
  • 1
  • 2
  • Possible duplicate: http://stackoverflow.com/questions/7024039/in-java-when-writing-to-a-file-with-dataoutputstream-how-do-i-define-the-endia – Lennart Jan 03 '13 at 10:52

3 Answers3

3

'Unicode' isn't a single format, but specifies encodings like UTF-16 (big and little-endian) and UTF-8.

You probably want something specific like UTF-16LE rather than Unicode. Have a look at the list in http://docs.oracle.com/javase/7/docs/api/java/nio/charset/StandardCharsets.html

artbristol
  • 32,010
  • 5
  • 70
  • 103
  • Note: it is Charset.UTF_16LE or "UTF-16LE" – Peter Lawrey Jan 03 '13 at 10:53
  • @PeterLawrey thanks for the correction. The underscore literal version works too for me; I guess it's an alias. – artbristol Jan 03 '13 at 10:56
  • Sorry, it is StandardCharsets.UTF_16LE. :] – Peter Lawrey Jan 03 '13 at 10:59
  • the generated file i have to commit to live scheduler which only accepts unicode format. In the old server the above code is running fine . in the new server only this problem is comming. do i need to install X11R6 – prasad Jan 03 '13 at 11:36
  • Is UTF-16BE a literal pass-thru mapping? i.e. if I am trying to OuterputStreamWriter.write binary data in a char [] will it pass it through without changing anything? – Michael Sep 23 '14 at 22:45
  • @Michael no. One of the 8-bit mappings might work (non-unicode), but why are you using a Writer to write binary data? – artbristol Sep 24 '14 at 08:16
1

If you use Unicode or UTF-16 it will be big endian by default. If you don't specify endianess, Java assumed big endian as a rule. If you want little endian you need to specify it with "UTF-16LE" or StandardCharsets.UTF_16LE

From java.nio.charset.StandardCharsets

public static final Charset UTF_16LE = Charset.forName("UTF-16LE");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • i have tried this also , the generated file is in in ANSI format. The application in running under linux sever. do i need to install X11R6 . – prasad Jan 03 '13 at 11:38
0

The Java charset name "UTF-16" means UTF-16 with big endian. Try something like this little endian format:

BufferedWriter out = new BufferedWriter
    (new OutputStreamWriter(new FileOutputStream(path),"UTF-16LE"));
live-love
  • 48,840
  • 22
  • 240
  • 204