1 / 26

Java Programming, Second Edition

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

maille
Download Presentation

Java Programming, Second Edition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java Programming, Second Edition Chapter Sixteen File Input and Output

  2. In this chapter, you will: Use the File class Understand data file organization and streams Use streams Write to and read from a file

  3. Write formatted file data Read formatted file data Use a variable file name Create random access files

  4. 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

  5. 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 creation

  6. 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 java.io.*

  7. Using the File Class • The java.io package contains all the classes you use in file processing • File class is a direct descendant of the Object class • Create a File object using a constructor that includes a filename • File someData = new File(“data.txt”);

  8. Understanding Data File Organization • Variables are stored in the computer’s main or primary memory, which is called RAM • 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

  9. Understanding Data File Organization • A field is a group of characters that has some meaning • Fields are grouped together to form records • Records are grouped to create files

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

  11. Understanding Streams

  12. 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

  13. Using Streams • Buffer- A small memory location that you use to hold data temporarily

  14. Writing to and Reading from a File • 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

  15. Writing to and Reading from a File • 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

  16. 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

  17. Writing Formatted File Data • The DataOutput interface includes methods such as: • writeBoolean() • writeChar() • writeDouble() • writeFloat() • writeInt()

  18. Reading Formatted File Data • DataInputStream objects enable you to read binary data from an InputStream • DataInput interface is implemented by DataInputStream

  19. Reading Formatted File Data • The DataInput interface includes methods such as • readByte() • readChar() • readDouble() • readFloat() • readInt() • readUTF()

  20. Using a Variable Name • You can provide a variable filename to a program using the command line

  21. 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

  22. 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

  23. 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

  24. Creating Random Access Files • Real-time applications • Require that a record be accessed immediately while a client is waiting • Require random access files

More Related