Title: CS 424524: Introduction to Java Programming
1CS 424/524 Introduction to Java Programming
- Lecture 10Spring 2004Department of Computer
ScienceUniversity of AlabamaJoel Jones
2Java IO
- Efficiency achieved by introducing a simple
buffer-oriented I/O model consisting of basic
streams of bytes and characters together with
monolithic, highly-tuned classes that combine
support for buffering, formatting, and parsing.
3Topics
- java.io
- File
- FileIntputStream
- BufferedInputStream
- BufferedReader
- FileOutputStream
- BufferedOuputStream
- BufferedWriter
- IOException
4java.io package
- Interfaces
- Classes
- Exceptions
5(No Transcript)
6This is the .05 Tour
- Java I/O By Elliotte Rusty Harold March 1999
1-56592-485-1, Order Number 4851594 pages,
39.95 US 58.95 CA 28.50 UK
7Topics Revised
- How do I list a directory?
- How do I read data from a file?
- How do I write data to a file?
- How do I append data to a file?
- How do I format numbers like C's printf()?
8How do I list a directory?
- The File Class
- Misnomer the File class does not necessarily
refer to a single file. It can represent the name
of a single file, or the names of a set of files.
- The method list() returns a String array of
file names
9Example
- try
- File path new File (.)
- String listing path.list()
- for(int count 0 count lt
- listing.length count)
- System.out.println(listcount)
-
- catch(Exception e)e.printStackTrace()
10How do I read data from a file?
- InputStream through inheritance, all derivatives
of InputStream have the basic method read() to
access a byte or an array of bytes - FilterInputStream Class
- The FilterInputStream offers a grab bag of
methods - Sun this is the base class for enhancing input
stream functionality - Eckel we couldnt figure out where else to put
this stuff, but it seemed like it belonged
together TIJ (9th ed.) - Avoid it--use InputStreamReader (JDK 1.1)
11Keyboard Access Use InputStreamReader
For reading keystrokes, use an InputStreamReader
Wrap with a BufferedReader decorator
- e.g.
- InputStreamReader iStreamReader new
InputStreamReader(System.in) - BufferedReader bReader new BufferedReader(iStrea
mReader)
12File Access Use FileInputStream
- BufferedReader in new
- BufferedReader(new
- FileInputStream(
- fileName))
13BufferedReader in Action
- String strInput ""
- try
- strInput bReader.readLine()
-
- catch (Exception e)
- System.err.println(Input Error
- e.toString())
-
14Reading Numbers etc.
- You dont
- You read strings and convert the strings to
numbers etc. - e.g.
- inputString input.readLine()
- midterm1 Integer.parseInt(inputString)
15Constructors and Methods
- BufferedReader(Reader) Create a buffering
character-input stream that uses a default-sized
input buffer. - BufferedReader(Reader, int) Create a buffering
character-input stream that uses an input buffer
of the specified size. - close() Close the stream.
- mark(int) Mark the present position in the
stream. - markSupported() Tell whether this stream supports
the mark() operation, which it does. - read() Read a single character.
- read(char, int, int) Read characters into a
portion of an array. - readLine() Read a line of text.
- ready() Tell whether this stream is ready to be
read. - reset() Reset the stream to the most recent mark.
- skip(long) Skip characters.
16Choices DataInputStream
- Allows you to read primitive types
- No readLine() (deprecated)
- read(byte) Reads data into an array of bytes.
- readBoolean() Reads a boolean.
- readByte() Reads an 8 bit byte.
- readChar() Reads a 16 bit char.
- readDouble() Reads a 64 bit double.
- readFloat() Reads a 32 bit float.
- readInt() Reads a 32 bit int.
- readLine() Reads in a line that has been
terminated by a \n, \r, \r\n or EOF. Deprecated. - readLong() Reads a 64 bit long.
- readShort() Reads a 16 bit short.
17How do I write data to a file?
- FileOutputStream decorated with a PrintWriter
- e.g.
- FileOutputStream fout new
- FileOutputStream("test.out")
- PrintWriter myOutput new
- PrintWriter(fout)
- myOutput.println("Hello There!")
18Dont forget try blocks!
- IOException
- try
- //open file(s)
-
- catch (IOException e)
- System.out.println(
- "Error opening file " e)
- System.exit(1)
19How do I append data to a file?
- just pass true as the second argument to this
FileOutputStream constructor to indicate that you
want to append data to the file - public FileOutputStream(String name, boolean
append)
20How do I format numbers like C's printf()?
- You dont. You build a string and print the
string. - the java.text package contains classes that
format numbers according to particular needs. In
particular it's worth getting to know the
java.text.NumberFormat and java.text.DecimalFormat
classes
21Efficiency BufferedWriter
- Wrap a BufferedWriter around FileWriters
- Example
- PrintWriter out
- new PrintWriter(
- new BufferedWriter(
- new FileWriter("foo.out")))
22Where to Get More Information
- http//java.sun.com/products/jdk/1.2/docs/api/java
/io/package-summary.html