- Forward


Character Input and Output in Java
An Introduction


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review of Console I/O
Back SMYC Forward
  • Standard Output:
    • System.out and System.err are PrintStream java.io.PrintStream objects that have print(), println(), and printf() methods
  • Standard Input:
    • System.in is an InputStream java.io.InputStream object that can be used to read bytes
General Character-Based I/O
Back SMYC Forward
  • Input:
    • Wrap any InputStream in a Scanner java.util.Scanner object that can be used to read double and int values and String objects or a BufferedReader java.io.BufferedReader that can be used to read String objects
  • Output:
    • Wrap any output stream in a PrintWriter java.io.PrintWriter
  • Both:
    • Call close() when done
Obtaining Other Input/Output Streams
Back SMYC Forward
  • The Most Common Channel:
    • Files
  • Other Important Channels:
    • Network connections
    • Memory
    • String objects
Input with a Scanner
Back SMYC Forward
  • Important Constructors:
    • Scanner(java.io.InputStream) java.util.Scanner#Scanner(java.io.InputStream)
    • Scanner(java.io.File) java.util.Scanner#Scanner(java.io.File)
  • Some Relevant Methods:
    • Scanner.useDelimiter(java.lang.String) java.util.Scanner#useDelimiter(java.lang.String)
    • Scanner.hasNextDouble() java.util.Scanner#hasNextDouble() , Scanner.hasNextInt() java.util.Scanner#hasNextInt() , and Scanner.hasNextLine() java.util.Scanner#hasNextLine()
    • Scanner.nextDouble() java.util.Scanner#nextDouble() , Scanner.nextInt() java.util.Scanner#nextInt() , and Scanner.nextLine() java.util.Scanner#nextLine()
Input with a Scanner (cont.)
Back SMYC Forward
  • An Important Issue:
    • The methods that read "up to" a delimiter do not consume newline characters
  • A "Work Around":
    • In some circumstances (when an int is the only thing on a line), you will need to invoke nextLine() immediately after invoking one of these methods
Input with a BufferedReader
Back SMYC Forward
  • The Steps::
    1. Construct a BufferedReader from an InputStreamReader or FileReader
    2. Read using the readLine() method (which throws IOException)
    3. Break the String object into parts (i.e., tokenize)
    4. Convert the parts (as necessary)
  • Two Approaches to Tokenizing:
    • StringTokenizer java.util.StringTokenizer
    • String.split(java.lang.String) java.lang.String#split(java.lang.String)
  • Converting:
    • Double.parseDouble(java.lang.String) java.lang.Double#parseDouble(java.lang.String) and Integer.parseInt(java.lang.String) java.lang.Integer#parseInt(java.lang.String)
Tokenizing Input
Back SMYC Forward
  • Tokenizing with split():
    • Is very convenient when every record contains every field
    • The length of the array can be used to handle situations with too few fields (but can't be used to identify which is missing)
  • Tokenizing with a StringTokenizer:
    • Is somewhat less convenient when every record contains every field (because it requries multiple calls to nextToken()
    • nextToken() throws NoSuchElementException which can be used to handle situations with too few fields
    • Can be told to return the delimiters so can be used to detect specific missing fields
An Example that Uses a Scanner (cont.)
Back SMYC Forward
javaexamples/iobasics/ScannerExample.java
 
An Example that Uses a BufferedReader (cont.)
Back SMYC Forward
javaexamples/iobasics/BufferedReaderExample.java
 
Reading Until End-of-Stream
Back SMYC Forward

Using a while Loop

javaexamples/iobasics/EndOfStreamExample.java (Fragment: while)
 
Reading Until End-of-Stream (cont.)
Back SMYC Forward

Using a do-while Loop

javaexamples/iobasics/EndOfStreamExample.java (Fragment: do)
 
Reading Until End-of-Stream (cont.)
Back SMYC Forward

Using an Elegant Idiom

javaexamples/iobasics/EndOfStreamExample.java (Fragment: elegant)
 
Output with a PrintWriter
Back SMYC Forward
  • Unformatted Output:
    • PrintWriter.print(java.lang.String) java.io.PrintWriter#print(java.lang.String) prints the String (i.e., sends it to the underlying OutpuStream)
    • PrintWriter.println(java.lang.String) java.io.PrintWriter#println(java.lang.String) prints the String followed by a '\n'
  • An Important Point:
    • Text sent to an output stream may be buffered
    • To force text through the buffer use PrintWriter.flush() java.io.PrintWriter#flush()
    • This is particularly important to be aware of when debugging
Output with a PrintWriter (cont.)
Back SMYC Forward
  • Format Strings:
    • Encapsulated in the Formatter java.util.Formatter class
  • One Way to use Format Strings:
    • Use the PrintWriter.printf(java.lang.String, java.lang.Object...) java.io.PrintWriter#printf(java.lang.String, java.lang.Object...) method to format the output
Formatting Output (cont.)
Back SMYC Forward
  • Another Way to Use Format Strings:
    • Convert everything into a formatted String java.lang.String before output
  • An Important Method:
    • The String.format(java.lang.String, java.lang.Object...) java.lang.String#format(java.lang.String, java.lang.Object...) method will do most of what you need (and works like printf())
Going Further: Other Ways to Format Output
Back SMYC Forward
  • Some Useful Classes:
    • NumberFormat java.text.NumberFormat
    • ChoiceFormat java.text.ChoiceFormat
    • MessageFormat java.text.MessageFormat
  • An Example:
    • javaexamples/iobasics/ChoiceFormatExample.java
       
Going Further: Internationalizing/Localizing Output
Back SMYC Forward
  • Internationalization:
    • Numbers, dates, currencies, etc... are often formatted diffferently in different countries
  • The Locale java.util.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.
Going Further: Internationalizing/Localizing Output (cont.)
Back SMYC Forward

An Example that Formats Numbers

javaexamples/iobasics/NumberFormatExample.java
 
Streams from Files
Back SMYC Forward
  • Reminder:
    • Files (or, really, file handles) are encapsulated in the File java.io.File class
  • Constructing Streams from File Objects:
    • FileInputStream(java.io.File) java.io.FileInputStream#FileInputStream(java.io.File)
    • FileOutputStream(java.io.File) java.io.FileOutputStream#FileOutputStream(java.io.File)
  • Convenience Constructors:
    • FileReader(java.io.File) java.io.FileReader#FileReader(java.io.File)
    • PrintWriter(java.io.File) java.io.PrintWriter#PrintWriter(java.io.File)
    • Scanner(java.io.File) java.util.Scanner#Scanner(java.io.File)
  • Note:
    • These constructors throw the checked exception java.io.IOException (they actually throw a more "specific" exception -- we'll learn what this means later)
Streams from Files (cont.)
Back SMYC Forward

An Example of File Input Using a BufferedReader

javaexamples/iobasics/FacultyRecordTokenizer.java
 
Streams from Files (cont.)
Back SMYC Forward

An Example of File Input Using a Scanner

javaexamples/iobasics/FacultyRecordScanner.java
 
Streams from Files (cont.)
Back SMYC Forward

An Example of File Output

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