Lecture 02 Object Oriented Programming - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

Lecture 02 Object Oriented Programming

Description:

Users use the term file to describe the objects that they ... long lastModified( ) Return the time the file was last modified. This time is system-dependent ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 15
Provided by: jaeki8
Learn more at: http://jsong.ba.ttu.edu
Category:

less

Transcript and Presenter's Notes

Title: Lecture 02 Object Oriented Programming


1
Lecture 13Streams
2
Introduction
  • Users use the term file to describe the objects
    that they stores on permanent storage devices
  • Data files contain facts and figures
  • Program files store software instruction
  • 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
  • Use the File class to gather information
  • Obtain information about file, such as whether it
    exists or is open, its size, and its last
    modification date, etc.

3
Introduction
  • Include the statement java.io.
  • Input and output contains all the classes used in
    file processing
  • Creating a file object
  • File someData new File (data.txt)
  • File someData new File (C\\temp\\data.txt)

4
File Class Methods
  • boolean canRead( )
  • Returns true if a file is readable
  • boolean canWrite( )
  • Return true if a file is writeable
  • boolean exists( )
  • Return true if a file exists
  • String getName( )
  • Return the files name
  • String getPath( )
  • Return the files path
  • long length( )
  • Returns the files size
  • long lastModified( )
  • Return the time the file was last modified. This
    time is system-dependent
  • Example 01 and Example 02

5
Data File
  • Character
  • Any one of the letters
  • Field
  • A group of characters
  • Records
  • A group of field
  • File
  • A group of record

Employee file
Andrews record
Brown record
ID 786
Brown
Jennifer
B
r
o
w
n
6
Data File
  • Before a program can use a file, the program must
    open the file and close the file when the program
    is finished.
  • When you leave a file open for no reason,
    computer resources will be suffered
  • In Java, when you perform an input operation in a
    program, you can picture bytes flowing into your
    program fro an input devices through a stream,
    and when you perform output, some bytes flow out
    of your program through another stream to an
    output devices

7
Streams
  • 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
8
Streams
Object
OutputStream
RandomAccessFile
InputStream
FileOutputStream
FilterOutputStream
FileInputStream
FilterInputStream
DataInputStream
BufferedInputStream
BufferedOutputStream
PrintStream
DataOutputStream
9
Streams
  • FileInputStream and OutputStream provide the
    capability to read from and to write to disk
    files
  • Create your own InputStream and OutputStream
    objects
  • assign System.in and System.out
  • Example Read from KB

10
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)

11
Writing to a File
  • Assign a file to the InputStream or OutputStream
  • Read data from the keyboard and store it
    permanently on a disk
  • Construct a FileOutputStream object and assign it
    to the OutputStream
  • Create a File object passing the filename to the
    File constructor. Then pass the File object to
    the constructor of the FileOutputStream class
  • Example Write file
  • Example Read file

12
Writing a Formatted File Data
  • Rather than reading a series of bytes, it is more
    useful to be able to read such a file in groups
    of bytes that constitute an integer, a String,
    and a double.
  • DataOutputStream objects enable us to write
    binary data to an OutputStream
  • The data with DataOutputStream objects is not
    readable in a text editor
  • Can read the data with a DataInputStream object
  • Methods
  • writeBoolean( ), writeChar ( ), writeDouble( ),
    writeFloat( ), writeInt( ), writeUTF()

13
Writing a Formatted File Data
  • Define a DataOutputStream object
  • DataOutputStream out
  • When you call the DataOutputStream constructor,
    pass a FileOutputStream object
  • out new DataOutputStream ( new FileOutputStream
    (someFile))
  • Example

14
Reading Formatted File Data
  • DataInputStream objects enable us to read binary
    data form an InputStream
  • readByte(), readChar(), readDouble(),
    readFloat(), readInt(), and readUTF()
  • DataInputStream in
  • In new DataInputStream(FileInputStream
    (somefile))
  • Example
Write a Comment
User Comments (0)
About PowerShow.com