0

Directly from this Java API:

write

public void write(int b)

Writes the specified byte to this stream. If the byte is a newline and automatic flushing is enabled then the flush method will be invoked.

Note that the byte is written as given; to write a character that will be translated according to the platform's default character encoding, use the print(char) or println(char) methods.

I see it's specified the byte is written as given; However if I try to write(65) I get the A I expected.

What should I write in write() in order to not match the same as it would be with print()? Could you do any example?

Kara
  • 6,115
  • 16
  • 50
  • 57
Rollerball
  • 12,618
  • 23
  • 92
  • 161
  • Binary 65 (as a byte) is the ASCII character `A`. – Sotirios Delimanolis Sep 05 '13 at 15:05
  • 1
    @Sotirios Delimanolis read the question again, he expects the 'A' –  Sep 05 '13 at 15:07
  • `write(65)` will output `A` into the stream; `print(65)` will output `65` into the stream. – Aleks G Sep 05 '13 at 15:08
  • I think `print(65)` will take the Unicode char 65 (0x41), convert it using the default character encodeing to byte(s) and then write those bytes. If its UTF-8, it will be `A`. In fact, in lots of character encoding will it be `A` – Lee Meador Sep 05 '13 at 15:12
  • @LeeMeador I would like to know the difference between "write the byte as it is" and using an encoding. "Byte as it is" I would expect something not encoded. However A is the result of an encoding – Rollerball Sep 05 '13 at 15:39
  • `print()` takes char or String types as parameters. These are Unicode. `write()` takes byte or byte[] types as parameters. These are just raw bits. Unicode can be decoded. Doing so ends up with one or more bytes. Those bytes can be encoded back to Unicode. `write(byte)` just transfers the bits to the file. `print(int)` considers the int to be a Unicode value, decodes it to a byte or bytes and transfers that byte or those bytes to the file. – Lee Meador Sep 05 '13 at 19:30
  • `print(0x0391)` will convert that Unicode character (a greek capital alpha) to bytes. I don't know what those bytes are but would guess its two bytes in many encodings. Those two bytes, if I'm right, will go to the file. – Lee Meador Sep 05 '13 at 19:32
  • so write(Alpha symbol) should write directly the bytes into the file right? – Rollerball Sep 06 '13 at 13:34

3 Answers3

0

Binary 01000001 (the byte) is the ASCII character A so you get that when you read as a String.

You would need to use FilterOutputStream.html#write(byte[])

write("65".getBytes());

to get the same output.

Or whatever type you need

write(new Integer(65).toString().getBytes());

What should I write in write() in order to not match the same as it would be with print()? Could you do any example?

System.out.println(65); // writes 65 as a String, String.valueOf(65).getBytes()
System.out.write(65); // writes 65 as a byte
System.out.flush();

prints

65 // the String value '65'
A // the character value of binary 65

Read the javadoc for the print methods. The one above accepts an int and

Prints an integer. The string produced by String.valueOf(int) is translated into bytes according to the platform's default character encoding, and these bytes are written in exactly the manner of the write(int) method.

In either case, bytes are written to the stream, but how they are displayed depends on the how your read them. String has constructors that accept a charsetName or Charset object. A Charset (as a concept and as a class) is

A named mapping between sequences of sixteen-bit Unicode code units and sequences of bytes.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

given

String s = "some string";
byte[] sData = s.getBytes();

write(sData, 0, sData.length); and print(s);

should be equivilent.

Cruncher
  • 7,641
  • 1
  • 31
  • 65
0

If you try

writer.print('Ä');

you might get varying results depending on your platform. It might be a single byte for a platform with Latin-1 as default encoding, or 2 bytes for a platform with UTF-8.

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112