Howdy! I was wondering if anyone could help me concerning some questions I have about Java I/O 1) On pg 587 in the 2nd edition of Bruce Eckel�s �Thinking in Java� he says The original intent of PrintStreams was to print all of the primitive data types and String objects in a viewable format. This is different from DataOutputStream whose goal is to put data elements on a stream in a way that DataInputStream can portably reconstruct them. Later on he says PrintStream [is concerned with] producing formatted output. While DataOutputStream handles the storage of data, PrintStream handles display.
What exactly does all this mean? I am still confused as to the differences between DataOutputStream and PrintStream. What does formatted output mean in this context? Is it that PrintStream saves the data in a human viewable format (i.e. a text file)? 2) What exactly is the reason for Reader and Writer classes? I understand that Input/OutputStreams focus on files at the byte level, while Reader/Writer classes focus on the char level, but who cares? Why can�t I just use a High Level stream linked to a Low Level stream to do everything that a Reader/Writer class can do? These classes seem completely superfluous. What am I missing? Thanks in advance Dan
1. Is it that PrintStream saves the data in a human viewable format (i.e. a text file)? YES. Also it isn't as easy to read the data back from data written with a PrintStream. 2. the Reader/Writer classes can handle various character encodings so they help in that regard.. From the JDK docs: An InputStreamReader is a bridge from byte streams to character streams: It reads bytes and translates them into characters according to a specified character encoding. The encoding that it uses may be specified by name, or the platform's default encoding may be accepted. This gives the ability to handle localized data easier than if you had to deal with the raw bytes.
Hi Scott, I used PrintStream to save the system output and error stream buffer, but PrintStream did not handle some special character e.g ("\n"), can you tell me how to fix that? Thanks! Mindy
posted 17 years ago
Could you explain what the problem was with newline characters? PrintStream can be a problem in some contexts since it uses the default character encoding and for the newline character that is very likely to cause some problems. Were you expecting linefeeds, CR/LF pairs or just CR? What did you get? Normal ASCII is pretty much going to come out the same on most platforms that use the roman alphabet - but line termination is an exception.
We should throw him a surprise party. It will cheer him up. We can use this tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database