Unformatted text preview:

Slide 1OutlineSlide 317.4 Sequential-Access Text FilesSlide 5Slide 617.4.2 Reading Data from a Sequential-Access Text FileSlide 8Slide 917.4.3 Case Study: A Credit-Inquiry ProgramSlide 11Updating a Sequential File ?The RandomAccessFile classSlide 1417.5 Object SerializationSlide 16Slide 17Slide 18Slide 19Slide 20Slide 21Slide 22Files and Streams(part 2)1-Based on slides from Deitel & Associates, Inc.- Revised by T. A. YangOutline•Text files•Binary files •Related Java classes: java.io.FileReader (for reading streams of characters)java.io.FileWriter (for writing streams of characters)java.io.File (for handling file attributes, folders, etc.)java.io.InputStream (for reading streams of raw bytes)java.io.FileInputStreamjava.io.ObjectInputStreamjava.io.OutputStream (for writing streams of raw bytes)java.io.FileOutputStreamjava.io.ObjectOutputStream 23417.4Sequential-AccessTextFiles•Sequential-access files store records in order by the record-key field. •Text files are human-readable files. •However, Java imposes no structure on a file.‾ Notions such as records do not exist as part of the Java language. ‾You must structure files to meet the requirements of your applications.5•Fig. 17.5, 17.6, and 17.7: CreateTextFileTest.htmAn application that creates and save records into a text file6•Sample output screen•Use command type clients.txt to display the content of a file.717.4.2ReadingDatafromaSequential-AccessTextFile• The application in Figs. 17.9 and 17.10 reads records from the file "clients.txt" created by the application of Section 17.4.1 and displays the record contents.8Q: When trying to run the application in NetBeans, the following error was displayed. Why and how to fix it?9•If a Scanner is closed before data is input, an IllegalStateException occurs. •If the data in the file are not properly formatted, a NoSuchElementException will be thrown.1017.4.3CaseStudy:ACredit-InquiryProgram•To retrieve data sequentially from a file, programs start from the beginning of the file and read all the data consecutively until the desired information is found. •It might be necessary to process the file sequentially several times (from the beginning of the file) during the execution of a program. •Class Scanner does not allow repositioning to the beginning of the file. –The program must close the file and reopen it.11•ACredit-InquiryProgram(Figures 17.11, 12, and 13): MenuOptionDemo−More complicated than what we have seen so far−Use the enum typeLinks: http://download.oracle.com/javase/tutorial/java/javaOO/enum.html http://javahowto.blogspot.com/2008/04/java-enum-examples.html12•The data in many sequential files cannot be modified without the risk of destroying other data in the file. •If the name “White” needed to be changed to “Worthington,” the old name cannot simply be overwritten, because the new name requires more space.•Fields in a text file — and hence records — can vary in size. •Records in a sequential-access file are not usually updated in place. Instead, the entire file is usually rewritten. •Rewriting the entire file is uneconomical to update just one record, but reasonable if a substantial number of records need to be updated.•Mater file versus transaction files: the merge operationUpdatingaSequentialFile?13TheRandomAccessFile class•To create a random access file, use the class java.io.RandomAccessFile.−The seek( ) method allows repositioning of the filePointer.•Example: RandomAccessFileDemo Demo of using RandomAccessFile class to create a file for both read and write operations•Exercise: Examine the content of the file test.out before and after running the application (next page), and explain why the content of the file was changed that way.141517.5ObjectSerialization•To read an entire object from or write an entire object to a file, Java provides object serialization. http://java.sun.com/developer/technicalArticles/Programming/serialization/ –Object serialization is the process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time. –The Java Serialization API provides a standard mechanism for developers to handle object serialization.•A serialized object is represented as a sequence of bytes that includes the object’s data and its type information. •After a serialized object has been written into a file, it can be read from the file and deserialized to recreate the object in memory.16•Classes ObjectInputStream and ObjectOutputStream, which respectively implement the ObjectInput and ObjectOutput interfaces, enable entire objects to be read from or written to a stream.•To use serialization with files, initialize ObjectInputStream and ObjectOutputStream objects with FileInputStream and FileOutputStream objects, respectively. •A demo application: ObjectInputOutputDemo.java // demo of saving some objects into a file using ObjectOutputStream and retrieving them bask using ObjectInputStream17•ObjectOutput interface method writeObject takes an Object as an argument and writes its information to an OutputStream. •A class that implements ObjectOuput (such as ObjectOutputStream) declares this method and ensures that the object being output implements Serializable. •ObjectInput interface method readObject reads and returns a reference to an Object from an InputStream. –After an object has been read, its reference can be cast to the object’s actual type.1817.5.1CreatingaSequential-AccessFileUsingObjectSerialization•Objects of classes that implement interface Serializable can be serialized and deserialized with Ob jectOutputStreams and Ob jectInputStreams.•Interface Serializable is a tagging interface. –It does not contain methods. •A class that implements Serializable is tagged as being a Serializable object. •An Obj ectOut putStream will not output an object unless it is a Serializable object.19•An application: AccountRecordSerializableDemo // Fig. 17.15 – 17.17// creates a binary fileclients.ser20•In a class that implements Serializable, every variable must be Serializable.•Any one that is not must be declared transient so it will be ignored during the serialization process.•All primitive-type variables are serializable. •For reference-type variables, check the


View Full Document

UHCL CSCI 3134 - Files and Streams

Download Files and Streams
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Files and Streams and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Files and Streams 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?