Streams

Hi.

I am wondering what the difference between byte and character streams are.

One thing I already know:

Byte Streams can read one single byte at a time while Character Streams read two bytes at a time (the number of bytes that compose the primitive type char).

Altough we have this great difference, I have two source files. the first using Byte streams and the second using character streams, both just read an input file and writes it out to another file.

They do the same thing nothing goes wrong. So what's the difference?

Thanks

[578 byte] By [charllescubaa] at [2007-11-27 8:46:27]
# 1

A byte stream is one dang byte after another.

A character stream is one dang character after another.

Now when it comes to *encoding* character streams as byte streams or

*decoding* byte streams to character streams, then you might find a character

corresponds to one, two or even more bytes, depending on the encoding.

As for what you are doing, copying a file, unless you are changing the character encoding, doing the copy byte-wise makes more sense.

BigDaddyLoveHandlesa at 2007-7-12 20:48:48 > top of Java-index,Java Essentials,Java Programming...
# 2

Character streams also apply a character encoding to the stream, so that the data is converted from bytes to characters correctly. If you read and write the stream using the same encoding, then you won't see any difference from using a byte stream. However, try reading it with one encoding, and writing it with another and see what difference it makes. The rule of thumb is, if it's text, use a character stream, if it's data, use a byte stream.

hunter9000a at 2007-7-12 20:48:48 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi bigdaddy.

Well I got what you mean.

Actually the prim. type Char in java follows Unicode Conventions, every char is made of 16bits(2bytes). I don't undestand how A byte stream can read half(8bits) and a half(8bits) of character and succeds.

So there is no difference between reading a file using byte or character strings?

It sounds strange to me.

charllescubaa at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 4
are you talking about input or output streams?Are you talking about the difference between streams and readers/writers?
tjacobs01a at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 5
What do you mean by data stream? Would it be binary ?Thanks
charllescubaa at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 6
Input and output streams:)
charllescubaa at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 7

> Hi bigdaddy.

>

> Well I got what you mean.

> Actually the prim. type Char in java follows Unicode

> Conventions, every char is made of 16bits(2bytes). I

> don't undestand how A byte stream can read

> half(8bits) and a half(8bits) of character and

> succeds.

>

> So there is no difference between reading a file

> using byte or character strings?

> It sounds strange to me.

There is a difference between reading data as bytes or characters. A char stream applies a character encoding. A byte stream doesn't.

Read through this IO tutorial, it explains things:

http://java.sun.com/docs/books/tutorial/essential/io/

hunter9000a at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 8
It is exactly the trail I am following.Maybe such explanation is in the end of the trail. Thnaks
charllescubaa at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 9

> Well I got what you mean.

> Actually the prim. type Char in java follows Unicode

> Conventions, every char is made of 16bits(2bytes). I

> don't undestand how A byte stream can read

> half(8bits) and a half(8bits) of character and

> succeds.

I'm not sure what you mean here. But consider how streams are connected

in order to read text from a file:

InputStream is= new FileInputStream(file);

Reader r = new InputStreamReader(is, "thecharsetname");

FileInputStream produces a stream of bytes, and InputStreamReader

converts this into a stream of chars. How many bytes are required

to generate a char depend on the charset encoding. For example, the

default windows encoding and the default UNIX encoding both have

one byte per char.

> So there is no difference between reading a file

> using byte or character strings?

> It sounds strange to me.

There is a difference -- reading a file as a character stream involves

decoding the byte stream into a char stream.

BigDaddyLoveHandlesa at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 10

> Actually the prim. type Char in java follows Unicode

> Conventions, every char is made of 16bits(2bytes). I

> don't undestand how A byte stream can read

> half(8bits) and a half(8bits) of character and

> succeds.

This used to be true in earlier versions of Unicode. Unicode is no longer a 16-bit character set.

In newer versions of Java Strings are not bound to the 16-bit characters; however, the Character class is still bound due to its encapsulated value being a primitive char.

See the Javadocs for Character pretty detailed explanation.

jbisha at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 11
Ok thank you
charllescubaa at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 12
Thanks BigIt is clearer now:)
charllescubaa at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 13
One more question BigIf I am not mistaken, char streams convert bytes to the local character set and from the local character set to bytes.In other words ... I can specify a characterset or not, Is it true?
charllescubaa at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 14

> One more question Big

> If I am not mistaken, char streams convert bytes to

> the local character set and from the local character

> set to bytes.

> In other words ... I can specify a characterset or

> not, Is it true?

Character streams use a default encoding if you don't specify one. That default may or may not match the actual encoding of the file, so it's always a good idea to specify.

hunter9000a at 2007-7-12 20:48:49 > top of Java-index,Java Essentials,Java Programming...
# 15

That is it. Now I know Why the examples I have here both worked using either byte and character streams

The contents of the file is US format. there are no special characters such as in portuguese (my language).

If I had tried to read a brazilian portuguese file, I would certainly have problems because the default charset of my linux is US.

tHANK YOU A LOT

charllescubaa at 2007-7-21 22:47:23 > top of Java-index,Java Essentials,Java Programming...