File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes BufferedInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "BufferedInputStream" Watch "BufferedInputStream" New topic


James Clarke
Ranch Hand

Joined: Oct 04, 2004
Posts: 148

I am just trying to understand how the following code works:

BufferedInputStream is = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[6 * 1024];
int byteRead;
while ((byteRead = != -1) {
output.write(buf, 0, byteRead);

how exactly does the BufferedInputStream write to an array of bytes ?
a byte only has 8 bits but a character has 16 bits, so how can a text file be read into a an array of bytes ?

thanks in advance,

Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
All files are comprised of an ordered sequence of bytes. There is no such thing as a "text file", unless you provide a definition of one beforehand, but that can be said for anything. Some other APIs implicitly provide that definition; for example,, which defines how bytes are converted to character data - maybe this fits your specific definition of "text file"? knows only of ordered bytes.

Tony Morris
Java Q&A (FAQ, Trivia)
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Moving to IO and Streams...

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
James Clarke
Ranch Hand

Joined: Oct 04, 2004
Posts: 148
Thanks for the response.

I would like to know exactly how the ordered sequence of bytes are read from a file. does anyone know of any resources that provide this information ?


Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Most OSs these days use the eight-bit byte as the basic unit of file structure. Ultimately, files contain bytes, and it's up to software to interpret what those bytes mean.

A significant portion of the world's computing is done using ASCII, a 7-bit code in which one byte corresponds to one character. Java is more sophisticated in that it uses a 16-bit character, which can represent most of the world's alphabets. But the OS still delivers bytes. It's up to Java to decide how to convert those bytes into characters. It does so using a character encoding. There are many possible encodings, and different ones are used in different parts of the world. The "UTF-8" encoding is a common one in the US -- it's basically the same as ASCII. Each byte is promoted to a character -- except for some special ones, which serve as signals to "shift" into a 3-byte encoding for special characters.

Anyway: the JDK Javadocs themselves contain a lot of information about character encodings. Read the pages for java.lang.Character,,, and the other pages those refer to.

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: BufferedInputStream
It's not a secret anymore!