Lecture 13 Streams - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 13 Streams

Description:

Declare class variables and write get methods to retrieve the variables' value ... System.err.println('Error opening file'); read() and write() method ... – PowerPoint PPT presentation

Number of Views:43
Avg rating:3.0/5.0
Slides: 16
Provided by: JSo87
Learn more at: http://jsong.ba.ttu.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture 13 Streams


1
Lecture 13Streams
  • Jaeki Song

2
Introduction
  • Store information from one execution of a project
    until the next execution
  • Use an object, store it, and then recreate the
    object exactly as it existed at a later time

3
Stream
  • The most basic of input/output with Java is done
    with stream
  • The stream can flow from the program to the
    screen, from a keyboard to the program, to/from a
    disk file or other storage media, to a printer,
    or even to a network or the Web
  • The stream commands enable you to write to a disk
    file or to the output screen

Java program
Input stream
Output stream
4
Output to the Screen
  • Send an output stream to the standard output
    device
  • Read an input stream from the standard input
    device
  • Java has three standard IO objects in, out, and
    err
  • System.out.println(Welcome)

5
Saving an Object in a File
  • Persistence
  • How can you make data persist from one run of the
    program to the next?
  • Write all data into a disk file and read it again
    for the next program execution
  • Object serialization
  • Java added a tool for persisting an entire object
    as a single unit
  • Using object serialization, you can use an output
    stream for writing objects
  • Creating a serializable class
  • Declare class variables and write get methods to
    retrieve the variables value

6
Writing an Object
  • Create a FileOuputStream object
  • Create an ObjectOutputStream object
  • Obtain the data from the text fields (after the
    user enters them)
  • Create an event for storing the data for one
    object
  • After all objects have been stored, close the
    FileOutputStream and ObjectOutputStream objects

7
The FileOuputStream Object
  • Establishes a link from the program to an actual
    disk file
  • FileOutputStream outputEmployee
  • Instantiate the object and assign the file name
    in a method
  • outputEmployee new FileOutputStream(Employee.tx
    t)

8
The ObjectOutputStream Object
  • To save an object using the stream, you must also
    set up an ObjectOutputStream class
  • ObjectOutputStream objSaveEmployee
  • objSaveEmployees new ObjectOutputStream(outputEm
    ployee)

9
Event for Storing Data
  • The writeObject method of the ObjectOutputStream
    class saves the contentes of the named object
  • Employee empCurrent new Employee(
    txtEmployeeName.getText(), txtHireDate.getText())
  • objSaveEmployee.writeObject(empCurrent)

10
Closing the Stream
  • When you are finished with FileOutputStream and
    ObjectOutputStream, you need to close both
  • objSaveEmployee.close() //close the object
    output stream
  • outputEmployee.close() // close the file output
    stream
  • Example EmployeeWrite

11
Files and Strem
  • Binary data I/O
  • DataInputStreams
  • Read binary data from InputStream
  • Methods read, readByte, readChar, readDouble...
  • DataOutputStreams
  • Write binary data to OutputStream
  • Methods write, writeChar, writeInt...

12
Reading an Object
  • Create a FileInputStream object
  • Obtain the data for one object from the file
  • Create an event to display the objects data in
    text fields

13
The FileInputStream and File Objects
  • The FileInputStream object associates the project
    with a file
  • The File object enables you to read the entire
    object with one read command
  • Creates a new File instance by converting the
    given pathname string into an abstract pathname
  • e.x) File inFile new File("employee.txt")

14
Example
//Declare stream objects FileInputStream
fis FileOutputStream fos public void
openStream() try File inFile
new File("employee.txt") File outFile new
File("employee_out.txt") fis new
FileInputStream(inFile) fos new
FileOutputStream(outFile)
catch(Exception error) System.err.println("
Error opening file")
15
read() and write() method
  • Methods in class FileInputStream and
    FileOutputStream
  • read( )
  • Reads a byte of data from this input stream. This
    method blocks if no input is yet available
  • Returns the next byte of data, or -1 if the end
    of the file is reached.
  • Write(int c)
  • Writes the specified byte to this file output
    stream.
  • c - the byte to be written
Write a Comment
User Comments (0)
About PowerShow.com