Download presentation
Presentation is loading. Please wait.
Published byRandell Taylor Modified over 8 years ago
1
Java Text I/O CS140 Dick Steflik
2
Reader Abstract super class for character based input Subclasses: – BufferedReader – CharArrayReader – FilterReader – InputStreamReader – PipedReader – StringReader
3
Writer Abstract super class for character based output Subclasses: – BufferedWriter – CharArrayWriter – FilterWriter – OutputStreamWriter FileWriter – PipedWriter – PrintWriter – StringWriter
4
Input Stream Abstract superclass for reading bytes Subclasses: – AudioInputStream – ByteArrayInputStream – FileInputStream – FilterInputStream – InputStream – ObjectInputStream – PipedInputStream – SequenceInputStream – StringBufferInputStream
5
OutputStream Abstract super class for byte oriented output Subclasses: – ByteArrayOutputStream – FileOutputStream – FilterOutputStream PrintStream – ObjectOutputStream – OutputStream – PipedOutputStream
6
When? Use Readers & Writers when dealing with characters – Text files, char arrays, Strings Use InputStreams and OutputStreams when dealing with bytes – Binary files (.doc,.xls), byte arrays, raw binary data
7
Scanner A simple text scanner that can parse primitive types and strings using regular expressions The default delimiter pattern is “whitespace” Breaks the input text up into tokens using the delimiter and returns the token or converts it to the target type Read JavaAPI on regular expressions (Google search “java regular expressions”
8
Regular Expressions pattern matching / delimitor | or $ ending char of string () defines scope ? zero or one of preceding * zero or more of preceding + one or more of preceding [abc] a or b or c. any single char {m,n} match preceding at least m times but not more than n
9
Examples Postal code ^\d{5}(-\d{4})?$ SSN (^\d{3}\d{2}\d{4}$)|(^\d{3}\-\d{2}\-\d{4}$)/ Money /^\d+(\.\d{0,2})$/
10
System.out.println During initialization (starting the JVM) the System class instantiates a PrintWriter as out; println is just a method of PrintWriter System.err is also a PrintStream initialized at startup of the JVM
11
System.in Is an InputStream typically connected to the keyboard and not used for input from other devices
12
Exceptions The normal way of handling errors in OO environments (Java and C++) Java has many Exceptions that can be thrown in response to error conditions The normal way of handling this is to catch the exception in the catch block of a try/catch statement The java compiler will tell you if you have a statement that needs to be in a try/catch
13
Errors Internal errors – usually fatal errors caused by machine conditions (OutOfMemory) Unchecked errors – usually caused by errors in your code (IndexOutOfRange, IllegalArguement Checked errors – something beyond your control has happened (FileNotFound)
14
try/catch any situation that may cause an unchecked error should be placed in the try portion of a try/catch you should code a catch for each exception that may be thrown Any method/function that an exception might be necessary need a “throws” clause added to the function definition
15
try/catch try/catch has an optional “finally” block that is used for “clean-up” of the situation Ex PrintWriter out = new PrintWriter(fn); try { writeData(out); } finally { out.close(); } clean-up could also be done in a catch but finally is cleaner
16
When I/O – networking – files – databases
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.