Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter.write(int c) Question  RSS feed

 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

I am trying to follow a tutorial.

They present there code #1:



And then they say that if you execute



then you can copy a file.

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 ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

A few more questions then:

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 ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Joseph Sweet:

I thought C's primitive sizes are compiler/OS dependent?


Yes, in theory, although it's a rare C compiler that doesn't use a 1-byte char, since char is the smallest addressable type. I've never seen one.
 
Joseph Sweet
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just updated my question :-)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!