prasad
prasad

Reputation: 11

Select file encoding in OutputStreamWriter

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?

Upvotes: 1

Views: 9030

Answers (3)

live-love
live-love

Reputation: 52524

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"));

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533880

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");

Upvotes: 1

artbristol
artbristol

Reputation: 32437

'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

Upvotes: 3

Related Questions