Introduction to Java 2 Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Java 2 Programming

Description:

Java I/O The Basics. Java I/O is based around the ... Java I/O Using ... Java I/O Using InputStreams. We've omitted exception handling in the ... – PowerPoint PPT presentation

Number of Views:66
Avg rating:3.0/5.0
Slides: 30
Provided by: Leigh86
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Java 2 Programming


1
Introduction to Java 2 Programming
  • Lecture 7
  • IO, Files, and URLs

2
Overview
  • Java I/O
  • The java.io package
  • Streams, Readers and Writers
  • Files
  • Working with Files
  • URLs
  • Working with Internet Resources

3
Java I/O The Basics
  • Java I/O is based around the concept of a stream
  • Ordered sequence of information (bytes) coming
    from a source, or going to a sink
  • Simplest stream reads/writes only a single byte,
    or an array of bytes at a time
  • Designed to be platform-independent
  • The stream concept is very generic
  • Can be applied to many different types of I/O
  • Files, Network, Memory, Processes, etc

4
Java I/O The Basics
  • The java.io package contains all of the I/O
    classes.
  • Many classes specialised for particular kinds of
    stream operations, e.g. file I/O
  • Reading/writing single bytes is quite limited
  • So, it includes classes which provide extra
    functionality
  • e.g. buffering, reading numbers and Strings (not
    bytes), etc.
  • Results in large inheritance hierarchy, with
    separate trees for input and output stream classes

5
Java I/O -- InputStream
6
Java I/O InputStreams
7
Java I/O Using InputStreams
  • Basic pattern for I/O programming is as follows
  • Open a stream
  • While theres data to read
  • Process the data
  • Close the stream

8
Java I/O Using InputStreams
  • I/O in Java
  • InputStream in new FileInputStream(c\\temp\\my
    file.txt)
  • int b in.read()
  • //EOF is signalled by read() returning -1
  • while (b ! -1)
  • //do something
  • b in.read()
  • in.close()

9
Java I/O Using InputStreams
  • But using buffering is more efficient, therefore
    we always nest our streams
  • InputStream inner new FileInputStream(c\\temp\
    \myfile.txt)
  • InputStream in new BufferedInputStream(inner)
  • int b in.read()
  • //EOF is signalled by read() returning -1
  • while (b ! -1)
  • //do something
  • b in.read()
  • in.close()

10
Java I/O Using InputStreams
  • Weve omitted exception handling in the previous
    examples
  • Almost all methods on the I/O classes (including
    constructors) can throw an IOException or a
    subclass.
  • Always wrap I/O code in trycatch blocks to
    handle errors.

11
Java I/O Using InputStreams
  • InputStream in null
  • try
  • InputStream inner new FileInputStream(c\\tem
    p\\myfile.txt)
  • in new BufferedInputStream(inner)
  • //process file
  • catch (IOException e)
  • e.printStackTrace()
  • finally
  • try in.close() catch (Exception e)

12
Java I/O OutputStream
13
Java I/O OutputStreams
14
Java I/O Using InputStreams
  • Basic pattern for output is as follows
  • Open a stream
  • While theres data to write
  • Write the data
  • Close the stream

15
Java I/O Using OutputStreams
  • Output in Java
  • OutputStream out new FileOutputStream(c\\temp\
    \myfile.txt)
  • while ()
  • out.write()
  • out.close()

16
Java I/O Using OutputStreams
  • OutputStream out null
  • try
  • OutputStream inner new FileOutputStream(c
    \\temp\\myfile.txt)
  • out new BufferedOutputStream(inner)
  • //write data to the file
  • catch (IOException e)
  • e.printStackTrace()
  • finally
  • try out.close() catch (Exception e)

17
But Thats Not All!
  • Input/OutputStream and sub-classes were part of
    Java 1.1.
  • Java 1.2 adds more classes specialised for
    character based I/O
  • The stream classes are for data I/O.
  • Classes for character I/O are called Readers and
    Writers
  • Why have specialised classes?
  • To support foreign languages

18
Unicode
  • Each character in the ASCII character set fits
    into a single byte
  • but thats not enough for chinese, and other
    complex alphabets
  • Need more than a single byte
  • A Java character (char) is 2 bytes
  • Java handles text using Unicode
  • International standard character set, containing
    characters for almost all known languages
  • And a few imaginary ones! (Klingon, Elvish)
  • Inside the JVM all text is held as Unicode

19
Java Text I/O
  • Because byte ! character for all languages, you
    have to turn bytes into chars using a
    Input/OutputStream
  • Java provides Readers and Writers to save you
    this work.
  • These classes deal with streams of characters
  • Read/write single character or array of
    characters
  • Again there are classes specialised for
    particular purposes

20
Java I/O Reader
21
Java I/O Readers
22
Using Readers
  • Reader in null
  • try
  • Reader inner new FileReader(c\\temp\\myfile.
    txt)
  • in new BufferedReader(inner)
  • //process file
  • catch (IOException e)
  • e.printStackTrace()
  • finally
  • try in.close() catch (Exception e)

23
Java I/O Writer
24
Java I/O Writers
25
Using Writers
  • Writer out null
  • try
  • Writer inner new FileWriter(c\\temp\\myfile.
    txt)
  • out new BufferedWriter(inner)
  • //write data to the file
  • catch (IOException e)
  • e.printStackTrace()
  • finally
  • try out.close() catch (Exception e)

26
Bridging the Gap
  • Sometimes you need to bridge across the two
    hierachies
  • Use InputStreamReader or OutputStreamWriter
  • InputStreamReader
  • Reads bytes from an InputStream, and turns them
    into characters using a character encoding
  • OutputStreamWriter
  • Turns characters sent to the Writer into bytes
    written by the OutputStream, again using a
    character encoding.

27
The File Object
  • Java provides access to the file system through
    the java.io.File object
  • Represents files and directories
  • Has methods for working with files and
    directories
  • Making directories, listing directory contents
  • renaming and deleting, checking permissions, etc
  • Check whether the File corresponds to a directory
    or a file with isDirectory()
  • Well-featured, and intuitive
  • Take a look through the javadocs
  • Quick example

28
The URL Object
  • Similar to File is the java.net.URL class
  • Provides access to information about website
    addresses
  • Most useful is a means to open an InputStream to
    a remote website
  • Use the openStream() method
  • Makes it very simple to retrieve files from the
    Internet.
  • Throws MalformedURLException if you provide an
    illegal internet address in its constructor
  • Example

29
URL Object Example
  • try
  • URL p new URL(http//www.ldodds.com/lectures/pe
    rson.jar)
  • InputStream inner p.openStream()
  • BufferedInputStream in new BufferedInputStream(i
    nner)
  • //process the file
  • in.close()
  • catch (Exception e)
  • e.printStackTrace()
Write a Comment
User Comments (0)
About PowerShow.com