aspose file tools*
The moose likes Beginning Java and the fly likes Byte and character streams Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Byte and character streams" Watch "Byte and character streams" New topic
Author

Byte and character streams

michael delta
Ranch Hand

Joined: Aug 11, 2009
Posts: 35

Now when we read a character stream it is always 2 bytes (transfering two by two or byte-byte?)
When we write in a binary stream,no data transformation occurs for example an int is written as 4byte(because that's what it is in ram), a character 2 bytes etc.
Also the integer 5 would be stored as 101. Why is 101 and not 00000000 00000000 00000000 00000101 as long as it commits 4 bytes per int?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

No, a character does not always take up two bytes when read from or written to a stream. How many bytes a character uses depends on the encoding.

If you use ASCII, a character is always 1 byte. If you use UTF-8, then the number of bytes per character is variable (at least 1 and at most 4 bytes).

In Java I/O, there are two kinds of classes: streams and readers / writers. Streams (InputStream and OutputStream) are used for reading and writing binary data (i.e. you use them to read the exact contents of a file on disk, for example). Readers and writers are used to read or write text. Readers and writers convert the bytes from and to characters, using a character encoding.

An int in Java is always 32 bits in memory, regardless of which bits are set to 0 and which bits are set to 1.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
michael delta
Ranch Hand

Joined: Aug 11, 2009
Posts: 35

How many bytes a character uses depends on the encoding.

So,java uses unicode(multibyte) but is able to manage ascii code(1 byte) also?Or always uses unicode and identifies the char size?

An int in Java is always 32 bits in memory, regardless of which bits are set to 0 and which bits are set to 1.

5 is 101,if i read an integer 5 i would read 3 bytes of zeros and a "byte of 5" right?

If i stop making these silly questions can i move on to I/O?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14268
    
  21

In memory, a char is a 16-bit Unicode character. But when you write that character to a file, it has to be converted to a representation, using a character encoding. ASCII is just one of the available character encodings. Ofcourse you can't encode all Unicode characters in ASCII - you'll lose information when you use ASCII.

michael delta wrote:5 is 101,if i read an integer 5 i would read 3 bytes of zeros and a "byte of 5" right?

An int is 32 bits = 4 bytes in memory, if the int would have the value 5 then that would be three bytes with a value of zero and one byte with a value 00000101 (in binary). But note that it's not as simple as that - the order in which those four bytes are stored can be different. Different kinds of processors store integers in a different order in memory, see endianness.
michael delta
Ranch Hand

Joined: Aug 11, 2009
Posts: 35

great,thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Byte and character streams