Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Java Programming, Second Edition Chapter Sixteen File Input and Output."— 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

9 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

10 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

11 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

12 Understanding Streams

13 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

14

15 Using Streams  Buffer- A small memory location that you use to hold data temporarily

16 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

17 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

18 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

19 Writing Formatted File Data  The DataOutput interface includes methods such as:  writeBoolean()  writeChar()  writeDouble()  writeFloat()  writeInt()

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

21 Reading Formatted File Data  The DataInput interface includes methods such as  readByte()  readChar()  readDouble()  readFloat()  readInt()  readUTF()

22 Using a Variable Name  You can provide a variable filename to a program using the command line

23 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

24 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

25 Creating Random Access Files The RandomAccessFile class contains the same read(), write() and close() methods as Input and OutputStreamThe 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 dataAlso 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 filesUse Java's RandomAccessFile class to create your own random access files

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


Download ppt "Java Programming, Second Edition Chapter Sixteen File Input and Output."

Similar presentations


Ads by Google