java - PrintStream vs PrintWriter -


i have searched site , have found answers, i'm having trouble understanding difference between these 2 classes. can explain differences between these 2 classes?

printstream original bridge deal encoding characters , other datatypes. if @ javadoc java.io.outputstream you'll see methods writing 2 distinct data types: byte , int.

in versions of jdk (1.0.x), when wanted write characters, 1 of 2 things, write bytes output stream (which assumed in system default character set):

outputstream.write("foobar".getbytes()); 

or wrap outputstream in printstream

printstream printstream = new printstream(outputstream); printstream.write("foobar"); 

see difference? printstream handling character conversion bytes, encoding (the constructor call above uses system default encoding, pass parameter). provides convenience methods writing double, boolean, etc....

in fact system.out , system.err defined printstream instances.

along comes jdk 1.1, , realize need better way deal pure character data, since printstream still has byte based methods writing. introduced "writer" abstract class deal strictly character, string , int data.

printwriter adds methods other types double, boolean, etc...

nowadays printwriter has format()/printf() methods format printing, etc...

as general rule, if you're writing character data, use writer instances. if you're writing binary (or mixed) data use outputstream instances.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -