Cup 8: File Input and Output - PowerPoint PPT Presentation

1 / 20
About This Presentation
Title:

Cup 8: File Input and Output

Description:

Used to output formatted representa-tions of objects to a text stream ... Members to be omitted from the object stream are declared transient ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 21
Provided by: timma87
Category:
Tags: cup | file | input | output | stream

less

Transcript and Presenter's Notes

Title: Cup 8: File Input and Output


1
Cup 8 File Input and Output
  • Special Topics Java

Dr. Tim Margush Department of Mathematics and
Computer Science The University of Akron
2
Streams
  • Character Stream
  • A sequence of Unicode characters
  • Unicode is automatically converted to and from
    the local host representation
  • Byte Stream
  • A sequence of Byte values
  • No translation
  • Binary mode

3
java.io Classes
  • File
  • A file or directory
  • OutputStream, InputStream
  • Base class for Byte streams
  • Writer, Reader
  • Base class for Character streams
  • RandomAccessFile

4
The File Class
  • Constructors
  • File(String fileOrPathName)
  • File(File directory, String fileName)
  • File(String directory, String fileName)
  • Once a File object is created, information may be
    obtained from the various methods

5
Selected File Methods
  • Boolean functions
  • exists()
  • isDirectory()
  • isFile()
  • canRead()
  • canWrite()
  • equals(File)
  • String getName()
  • String getPath()
  • String list()
  • returns an array of filenames if the object
    represents a directory
  • long length()
  • number of bytes in the file

6
File Modifications
  • delete()
  • deletes the file represented by the current File
    object
  • renameTo(FilePath)
  • renames and possibly moves the file named by the
    current object
  • mkdir() and mkdirs()
  • creates a directory (perhaps a whole path) named
    by the current object

7
OutputStream Methods
  • write(int b)
  • write(byte b)
  • write(byte b, int offset, int length)
  • flush()
  • close()
  • These methods define functionality - they are
    generally overridden in subclasses

8
OutputStream Derivations
  • FileOutputStream
  • stream is directed to a file
  • ByteArrayOutputStream
  • stream is directed to a byte array
  • PipedOutputStream
  • stream is intended to connect to a
    PipedInputStream
  • FilterOutputStream
  • a base class for further capabilities

9
Data and Buffered OutputStream Classes
  • Derived from FilterOutputStream
  • DataOutputStream
  • used to write primitive data in binary mode - no
    translation
  • BufferedOutputStream
  • adds buffering to improve performance
  • Constructors require an object of one of the
    other OutputStream types

10
DataOutputStream
  • Used when writing data that is not strictly
    character data
  • creates a sequence of bytes corresponding to the
    internal representation of data
  • methods exist to write all of the primitive types
    as well as a String
  • results are usually not human readable

11
Character Output Streams
  • StringWriter
  • CharArrayWriter
  • PipedWriter
  • OutputStreamWriter
  • PrintWriter
  • provides String output for other Writers
  • BufferedWriter
  • buffers other Writers
  • FilterWriter
  • None of these are directly related to a file
  • FileWriter
  • derived from OutputStreamWriter
  • writes data to a File named in the constructor

12
PrintWriter
  • Constructed from a Writer or an OutputStream
    object
  • Used to output formatted representa-tions of
    objects to a text stream
  • Includes print() and println() methods
  • these are overloaded for all of the basic types
    and will output any object using its toString
    method

13
Formatting Output
  • java.text contains numerous classes dealing with
    text - data conversions
  • Support is provided for internationalization via
    getLocale()
  • After creating a format object, it may be used to
    format data (convert to String representations)

14
Input Streams
  • A collection of classes parallel the Output
    stream classes, beginning with InputStream at the
    top of the hierarchy
  • FileInputStream
  • SequenceInputStream
  • PipedInputStream
  • ByteArrayInputStream
  • FilterInputStream
  • ObjectInputStream

15
FilterInputStream
  • DataInputStream
  • support for basic data types (binary)
  • BufferedInputStream
  • add buffering to existing stream
  • LineNumberInputStream
  • count lines of input
  • PushBackInputStream
  • allow bytes to be placed back into the stream for
    rereading
  • plus Additional classes providing support for
    data decompression and security

16
Character Input Streams
  • Derived from Reader
  • Provides automatic characer code conversion from
    native encoding method
  • StringReader
  • CharArrayReader
  • PipedReader
  • InputStreamReader
  • FileReader
  • BufferedReader
  • LineNumberReader
  • FilterReader
  • PushBackReader

17
Formatted Input
  • java.io.StreamTokenizer
  • used to split character data into meaningful
    tokens that can be parsed by other methods
  • method nextToken() parses the next token of the
    Stream and places its value in a public instance
    variable
  • double nval or String sval
  • TT_NUMBER, TT_WORD, TT_EOF, TT_EOL

18
StringTokenizer
  • java.util.StringTokenizer
  • nextToken() method returns a substring with no
    whitespace
  • BufferedReader contains readLine() method
  • Conversion
  • Integer.parseInt(x) //decimal
  • Integer.parseInt(x,16) //hex input
  • etc

19
Object I/O - Serialization
  • ObjectOutputStream
  • created from a DataOutputStream
  • contains writeObject() method
  • Object must implement the Serializable Interface
  • ObjectInputStream
  • created from a DataInputStream
  • contains readObject() method
  • Object must be Serializable

20
implements Serializable
  • This one is eay - there are no special methods
    that you are required to implement!
  • A default constructor must exist
  • All the data members must be a basic data type,
    or an object that is itself Serializable
  • Members to be omitted from the object stream are
    declared transient
Write a Comment
User Comments (0)
About PowerShow.com