- Forward


Character Input and Output in Java
Intermediate Topics


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Background
Back SMYC Forward
  • Raw I/O:
    • A way to read bytes from a stream
  • The Limitations:
    • Converting from bytes to characters is complicated because of the different encoding schemes
Motivation
Back SMYC Forward
  • Character-Based I/O:
    • Reader and Writer classes that can be used to decorate InputStream and OutputStream objects
  • Providing Additional Functionality:
    • Use the decorator pattern again (however, since there is no interface at the top, each decorator both extends its parent and delegates important operations to its parent)
Some Classes for Character-Based Output
Back SMYC Forward
writers_java
Some Classes for Character-Based Input
Back SMYC Forward
readers_java
One Way to Use Readers
Back SMYC Forward
  1. Get an InputStream
    • System.in
    • From a File (by constructing a FileInputStream )
    • From a URL (using the openStream method)
    • From a Socket (using the getInputStream method)
  2. Create an InputStreamReader (from the InputStream )
  3. Create a BufferedReader (from the InputStreamReader )
One Way to Use Writers
Back SMYC Forward
  1. Get an OutputStream
    • System.out
    • From a File (by constructing a FileOutputStream )
    • From a Socket (using the getOutputStream method)
  2. Create an OutputStreamWriter (from the OutputStream )
  3. Create a PrintWriter (from the OutputStreamWriter )
A Simple Example
Back SMYC Forward
javaexamples/iobasics/WriterReaderExample.java
 
The Benefits of Decorating Streams
Back SMYC Forward

An Example

javaexamples/iobasics/ReaderExample.java
 
Processing Text Output
Back SMYC Forward
  • The Approach You've Probably Seen:
    • The Concept - Provide formatting information to the output method
    • The Participants - The printf() method in the PrintWriter class
  • Another Approach:
    • The Concept - Provide formatted String objects to the output method
    • The Participants - NumberFormat , ChoiceFormat , the format() method in
    • the String class, and the print()/println() methods in the PrintWriter class
Processing Text Output (cont.)
Back SMYC Forward

An Example that Formats "Choices"

javaexamples/iobasics/ChoiceFormatExample.java
 
Processing Text Output (cont.)
Back SMYC Forward
  • Internationalization:
    • Numbers, dates, currencies, etc... are often formatted diffferently in different countries
  • The Locale Class:
    • An encpasulation of a geographical, political or cultural region that can be used for locale-sensitive tasks
  • International Standards:
    • ISO Language Codes Click here for related information from another site.
    • ISO Country Code Click here for related information from another site.
Processing Text Output (cont.)
Back SMYC Forward

An Example that Formats Numbers

javaexamples/iobasics/NumberFormatExample.java
 
Processing Text Input
Back SMYC Forward
  • An Approach You've Seen:
    • Use a Scanner object and its next___() methods
  • Shortcomings of this Approach:
    • "Unexpected" consumption (or lack thereof) of newline characters
    • Not robust with respect to formatting defects
Processing Text Input (cont.)
Back SMYC Forward
  • A More Robust Approach:
    • Only read String objects
    • Break the String into parts and convert the parts
  • Some Relevant Classes/Methods:
    • The split() method in the String class and the StringTokenizer class
    • Double.parseDouble(java.lang.String) and Integer.parseInt(java.lang.String)
Processing Input (cont.)
Back SMYC Forward

Using the StringTokenizer

javaexamples/iobasics/FacultyRecordTokenizer.java
 
Processing Input (cont.)
Back SMYC Forward

Using the parse___() Methods

javaexamples/iobasics/ExpressionTokenizer.java
 
Processing Input (cont.)
Back SMYC Forward
  • Similarities of String.split() and StringTokenizer:
    • Tokenize a String
  • Differences between String.split() and StringTokenizer:
    • split() uses regular expressions, StringTokenizer uses a list of delimiters
    • split() returns an array, StringTokenizer is an iterator
    • StringTokenizer can return the delimiters as tokens
Processing Input (cont.)
Back SMYC Forward

A Comparison of StringTokenizer and String.split()

javaexamples/iobasics/BufferedReaderExample.java
 
There's Always More to Learn
Back -