Both DataOutputStream and PrintStream do basically the same thing - convert primitive types and Strings to bytes before writing them (writeUTF() is an exception). Why different classes ?

DataOutputStream outputs data in its raw binary form - eg always 4 bytes for an int
PrintStream outputs data in a human-readable character format - so an int could be anything such as "0" or "-1234567"

Thanks. Does that mean that if we write the integer 2 using DataOutputStream, it'll write 0x0002, whereas if we use PrintStream, it'll convert it into the unicode code 0x32 and then use the UTF-8 or whatever is the default encoding of the platform to write that to the file ? Am i right ?

Yes (except that ints in Java are 4 bytes, 0x00000002)

Thanks.

The differences between the two classes are twofold: a DataOutputStream will write numeric data in a binary format, while the PrintStream will provide a human readable form. The methods of a DataOutputStream will throw exceptions, while for the PrintStream I/O errors are treated internally and can only be accessed with a special method checkErrors(). This is why in the standard phrase System.out.println you do not have to care about exceptions

Hello hajjanmu
Welcome to Daniweb.
I guess you did notice that this thread was marked "solved" 2 years ago?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.