1 / 6

CIS 270—App Dev II

CIS 270—App Dev II. Big Java Chapter 19 Files and Streams. 19.1 Text and Binary Formats I. Data can be stored in text format Text is a human-readable sequence of ____________ The integer 12,345 is stored as the following sequence of Unicode characters: ‘1’ ‘2’ ‘3’ ‘4’ ‘5’

suzy
Download Presentation

CIS 270—App Dev II

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. CIS 270—App Dev II Big Java Chapter 19 Files and Streams

  2. 19.1 Text and Binary Formats I • Data can be stored in text format • Text is a human-readable sequence of ____________ • The integer 12,345 is stored as the following sequence of Unicode characters: ‘1’ ‘2’ ‘3’ ‘4’ ‘5’ • Text I/O is easy for humans • Reader and _________ classes (and their subclasses) used for text input/output • To read text data from a file in Java, FileReader reader = new FileReader(“input.txt”); • To write text data to a file in Java, FileWriter writer = new FileWriter(“output.txt”); • The Readermethod _______ reads characters characters Writer read()

  3. 19.1 Text and Binary Formats II bytes • Data can also be stored in binary format • Binary is a machine-readable sequence of ________ • The integer 12,345 is stored as the following sequence of 4 bytes: 00000000 00000000 00011000 00111001 • Binary I/O is more efficient for digital computers • InputStreamand ____________ classes (and their subclasses) are used for binary input/output • To read binary data from a file in Java, FileInputStreaminputStream= new FileInputStream (“input.bin”); • To write text data to a file in Java, FileOutputStreamoutputStream= new FileOutputStream (“output.txt”); • The InputStreammethod _______ reads bytes OutputStream read()

  4. 19.2 An Encryption Program • The CaesarEncryptor class gets the input and output file names, and the key, from the user, creates input/output _______ for the files, creates a CaesarCipher object, which then calls its encryptStream() method. • The CaesarCipherclass reads a character from an input file, applies an encryption _____, and writes the encrypted character to an output file. • int next = in.read(); • byte b = (byte) next; • byte c = (byte) (b + key); • out.write(c); streams key

  5. 19.3 Random (Direct) Access • It is more efficient to directly access a specific data record in a file than to sequentially read/write all records. • To know where to go in a file, all data fields must be of ______ size large enough to hold each item. • ________ format is better for records of fixed size. • Code examples: • (int) file.length()/RECORD_SIZE // number of records • file.seek(n * RECORD_SIZE) // move to nth record • file.readInt() // read the next int in that record fixed Binary

  6. 19.4 Object Streams objects • A program can write data fields separately or can write entire ________ at once (binary format). • BankAccount b = new BankAccount(); • ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream( “bank.dat” ); • out.writeObject( b ); • Reading objects is similar. • ObjectInputStream in = new ObjectInputStream( new FileInputStream( “bank.dat” ); • BankAccount b = (BankAccount) in.readObject(); • Another technique is to store several objects in an array and then store that array and save it. • If objects use streams, the class must implement the Serializableinterface (objects have _____ numbers). serial

More Related