Java Programming, Second edition by HC12110721139


									Java Programming,
    Second Edition

      Chapter Sixteen
   File Input and Output
In this chapter, you will:
  Use the File class
  Understand data file
   organization and streams
  Use streams
  Write to and read from a file
 Write formatted file data
 Read formatted file data
 Use a variable file name
 Create random access files
         Using the File Class
 File- Describes the objects that computers
 store on permanent storage devices such
 as hard, floppy or zip drives, reels of
 magnetic tape, or compact disks
   Data files contain facts and figures
   Program files, also called applications, store
    software instructions
   Other files can store graphics, text, or
    operating system instructions
         Using the File Class
 Files share common characteristics
   Each file occupies a section of disk or other
    storage device
   Each file has a name and a specific time of
           Using the File Class

 File class- To gather file information
 File class does not provide any opening,
  processing, or closing capabilities for files
 Use the File class to obtain information about a
  file, such as whether it exists or is open, its size,
  and its last modification date
 Must include the statement
    import*
         Using the File Class

 The package contains all the classes
  you use in file processing
 File class is a direct descendant of the Object
 Create a File object using a constructor that
  includes a filename
   File someData = new File(“data.txt”);
       Understanding Data File
 Variables are stored in the computer’s
  main or primary memory, which is called
 When you need to retain data for a long
  amount of time, save the data on a
  permanent or secondary storage device
  such as a floppy disk, hard drive, or
  compact disk
       Understanding Data File
 A field is a group of characters that has
  some meaning
 Fields are grouped together to form
 Records are grouped to create files
      Understanding Streams
 Java views files as a series of bytes
 You can picture those bytes flowing into
  and out of your program through a stream,
  or a pipeline
 A stream is an object, and like all objects,
  streams have data and methods
Understanding Streams
             Using Streams
 InputStream and OutputStream are
 abstract classes that contain methods for
 performing input and output
   Must be overridden in their child classes
   InputStream and OutputStream are
    subclasses of the Object class
            Using Streams
 Buffer- A small memory location that you
 use to hold data temporarily
  Writing to and Reading from a
 Instead of assigning files to the standard input
  and output devices, you can also assign a key
  file to the InputStream or OutputStream
    For example, you can read data from the keyboard
     and store it to a disk
 You can construct a FileOutputStream object
  and assign it to the OutputStream
 Writing to and Reading from a
 You can associate a File object with the
 output stream in two ways
   You can pass the filename to the constructor
    of the FileOutputStream class
   You can create a File object passing the
    filename to the File constructor; then pass the
    File object to the constructor of the
    FileOutputStream class
   Writing Formatted File Data
 Use the DataInputStream and
  DataOutputStream classes to accomplish
  formatted input and output
 DataOutputStream objects enable you to
  write binary data to an OutputStream
 When you use a DataOutputStream
  connected to FileOutput Stream, this is
  known as chaining the stream objects
   Writing Formatted File Data
 The DataOutput interface includes
 methods such as:
     writeBoolean()
     writeChar()
     writeDouble()
     writeFloat()
     writeInt()
  Reading Formatted File Data
 DataInputStream objects enable you to
  read binary data from an InputStream
 DataInput interface is implemented by
  Reading Formatted File Data
 The DataInput interface includes methods
 such as
     readByte()
     readChar()
     readDouble()
     readFloat()
     readInt()
     readUTF()
      Using a Variable Name
 You can provide a variable filename to a
 program using the command line
 Creating Random Access Files
 Sequential access files access records in
  sequential order from beginning to end
 Batch processing involves performing the same
  tasks with many records, one after the other
 Creating Random Access Files
 Random access files are files in which records
  can be accessed in any order
   Also called direct access files
   More efficient than sequential access files
Creating Random Access Files
• The RandomAccessFile class contains the
  same read(), write() and close() methods as
  Input and OutputStream
• Also contains seek() that lets you select a
  beginning position within the file before
  reading or writing data
• Use Java's RandomAccessFile class to create
  your own random access files
 Creating Random Access Files
 Real-time applications
   Require that a record be accessed
    immediately while a client is waiting
   Require random access files

To top