Files and Steams - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Files and Steams

Description:

To store information beyond the operation of the computer, we use some form of ... Anybody who has used the web has used URLs. E.g. http://www.ecst.csuchico.edu/~jlong ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 30
Provided by: Jim455
Category:
Tags: anybody | boils | files | steams

less

Transcript and Presenter's Notes

Title: Files and Steams


1
Files and Steams
  • MINS 116
  • Spring 2000
  • Java Basics

2
Topics
  • Files
  • Files in Java
  • Streams
  • Exceptions
  • URLs

3
Files
  • All computer operation boils down to binary.
  • 0 - The circuit is open
  • 1 - The circuit is closed

4
Files
  • All information handled by computers is
    represented in binary.
  • Numbers
  • Strings of information
  • Images
  • Java code

5
Files
  • Information is only viable while the computer has
    power.
  • To store information beyond the operation of the
    computer, we use some form of secondary storage.

6
Files
  • With secondary storage, the streams of 1s and 0s
    are put on some non-volatile medium.
  • Tape (paper with punched holes, magnetic)
  • Disk (magnetic, optical)
  • Others?

7
Files, Streams and Java
  • Java supports files through package java.io.
  • java.io.File represents a file.
  • A Stream must be associated with a Java file to
    extract information.
  • Java system reserved files are System.in,
    System.out, System.err

8
Files, Streams and Java
  • Example
  • // Open a file
  • File exampleFile new File( testFile.txt)
  • // Associate a stream
  • FileInputStream inputStream new
    FileInputStream( exampleFile )

9
Files, Streams and Java
  • To correctly process a file, you need to have
    knowledge about the structure of the information
    contained in the file.
  • Example
  • If you open a file and read a char, but
    originally written is an integer, what results
    would you expect?

10
File Types
  • Sequential
  • Sequential access of records in file
  • Open for Read or Write access
  • Random
  • Fixed length records
  • Wasted disk space
  • Binary
  • Byte stream
  • A lot of program responsibility

11
File Types
  • In Java, File represents the file as located in
    secondary storage
  • Streams, Readers, and Writers represent the
    access for reading and writing of the file.

12
Stream/File Types in Java
  • Sequential - all files in Java start as
    sequential.
  • Random Access - class RandomAccessFile
  • Binary - class ByteArrayInputStream

13
Readers and Writers
  • Readers - Reader provides for reading of
    character streams. Specialized readers are
  • BufferedReader
  • LineNumberReader
  • CharArrayReader
  • InputStreamReader
  • FileReader
  • FilterReader
  • PushbackReader
  • PipedReader
  • StringReader

14
Readers and Writers
  • Writers - Writer provides for writing of
    character streams. Specialized writers are
  • BufferedWriter
  • CharArrayWriter
  • OutputStreamWriter
  • FileWriter
  • FilterWriter
  • PipedWriter
  • StringWriter

15
Streams - Input and Output
  • Streams provide for inputting and outputting
    streams of bytes as opposed to the character
    strings of writers.

16
Streams - Input and Output
  • InputStreams
  • BufferedInputStream
  • ByteArrayInputStream
  • DataInputStream
  • FileInputStream
  • FilterInputStream
  • LineNumberInputStream
  • ObjectInputStream
  • PipedInputStream
  • PushbackInputStream
  • SequenceInputStream
  • StringBufferInputStream

17
Streams - Input and Output
  • OutputStreams
  • BufferedOutputStream
  • ByteArrayOutputStream
  • DataOutputStream
  • FileOutputStream
  • FilterOutputStream
  • ObjectOutputStream
  • PipedOutputStream
  • Why more input streams than output streams?

18
A brief word on exceptions...
  • To deal with files and streams in Java,
    exceptions will nee to be handled.
  • An exception is an asynchronous error event which
    will disrupt the flow of processing.

19
A brief word on exceptions...
  • Exceptions are handled in a try-catch block.
  • try
  • // do something which may throw an
    exception
  • catch ( Exception exp )
  • // handle the error condition
  • ...

20
A brief word on exceptions...
  • If a method has the capability to throw an
    exception, your source code must either
  • 1) Catch the exception in a catch block.
  • 2) Throw the exception itself.

21
A brief word on exceptions...
  • Example (try-catch)
  • File myFile null
  • String path null
  • try
  • myFile new File ( path )
  • // Begin the processing
  • ...
  • catch ( NullPointerException exp )
  • // We end up here if since path is null

22
A brief word on exceptions...
  • Example (throwing)
  • public void myMethod ( )
  • throws NullPointerException
  • File myFile null
  • String path null
  • myFile new File ( path )
  • // Begin the processing
  • ...
  • Processing will never begin since new File( path
    ) will cause an exception to be thrown.

23
URLs
  • U - Uniform
  • R - Resource
  • L - Locator

24
URLs
  • Anybody who has used the web has used URLs.
  • E.g. http//www.ecst.csuchico.edu/jlong

25
URLs
  • A URL provides a standardized method for locating
    resources over the web.
  • URLs can reference
  • http - Hyper Text Transfer Protocol
  • file - Simple File access

26
URLs
  • Anatomy of a URL
  • http//www.ecst.csuchico.edu/jlong/mins116.html
  • http - information transfer protocol.
  • www.ecst.csuchico.edu - host machine name.
  • jlong/mins116.html - the file component.

27
URLs
  • Rather than using file name, which need to be
    local to the machine on which the Java code is
    executing, we can use URLs to gain access to
    information.

28
URLs
  • URLs provide
  • Location Transparency of Information
  • Standardized naming
  • A input stream created from a URL need not be on
    a local machine.
  • Using URLs allows our programs to be used in a
    networking environment.

29
Summary
  • Files are used to store persistent information on
    secondary storage devices.
  • In Java, file access is performed via Streams.
  • To successfully use Files and Streams, exceptions
    must be handled.
  • URLs can provide location transparency for our
    information sources.
Write a Comment
User Comments (0)
About PowerShow.com