Well, I don't see why it would work. Systen.in is an InputStream. So when you do InputStream.read() it reads a byte from the source but you get an int (4 bytes). System.out is a PrintWriter. The API for PrintWriter.write(int c) says: "Writes a single character". So it writes to the output a character, which is 2 bytes. Hummm... doesn't it look strange? in the original file, for each byte we get in the output file 2 bytes. This is not what I would call copying a file. What do you think. Does it work or not?
Also, they present code #2:
Which you execute like this:
Now, this one I do understand. InputStream.read() reads a byte and give you an int (4 bytes). According to the API, OutputStream.write(int b) does this: "Writes the specified byte to this output stream". So OutputStream.write(int b) truncates the int parameter, and writes to the output only one byte (the LS byte). Great. Read one byte, write one byte. Just like I would expect when I copy a file.
What about code #1? [ July 05, 2007: Message edited by: Joseph Sweet ]
The use of "character" instead of "byte" in the PrintStream Javadocs is really just fanciful; the method writes a single byte. When those Javadocs were originally being written, even though Java was being designed to use 16-bit characters, lots of people still thought of "byte" and "char" as being the same size, as they traditionally are in the C language.
1. Are Writers (such as PrintWriter) not supposed to do character processing (as opposed to byte processing, which is what Input/OuuputStreams do). If so, how come the PrintWriter has that write(int c) method which you say actually writes a byte instead of a char.
2. What about the other PrintWriter's write methods, such as: write(char buf) which according to the API "Writes an array of characters". Does it also actually write an array of bytes?
3. I thought C's primitive sizes are compiler/OS dependent? [ July 06, 2007: Message edited by: Joseph Sweet ]