This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Transfer Learning for Natural Language Processing (MEAP) and have Paul Azunre on-line!
See this thread for details.
Win a copy of Transfer Learning for Natural Language Processing (MEAP) this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

PrintWriter.write(int c) Question

 
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 ]
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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 :-)
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic