Docsity

Java I/O: Reading and Writing Files as Byte and Character Streams, Slides of Applications of Computer Sciences

Partial preview of the text

Download Java I/O: Reading and Writing Files as Byte and Character Streams and more Slides Applications of Computer Sciences in PDF only on Docsity!

Files, Applets, & Exceptions

Chapter 12 Overview

Today’s Lesson Plan

  • Discuss labs, quizzes, and TDA topics (10 min)
    • Midterm results will be posted tomorrow
  • Class Activities – covers Chapter 12 (150 min)
    • Discuss Files. (30 min)
    • Discuss Streams. (30 min)
    • Discuss Sequential Access files. (30 min)
    • Discuss Character-Based Files. (30 min)
    • Discuss Byte-Based Files. (15 min)
    • Discuss Random Access files. (15 min)
  • Coding Exercises & break (90 min)
    • Code Ex 1 – p 435 – #8.5.13a & 13b
    • Code Ex 2 – p 629 – #12.2.
    • Code Ex 3 – p 631 – #12.2.16a & 16b Docsity.com

Streams

  • In Java, streams are simply sequences of bytes
    • Transfer data items in succession
  • We can read a byte stream from an input stream object
  • We can write a byte stream to an output stream object
  • Typically input and output stream objects can be files but they also can be network connections
  • This generality means we can use the same functions for accessing networks as reading files

The standard Java streams

System.in

System.err

System.out

Streams

  • Java distinguishes between two types of streams:
  • Text streams, containing “characters“ (they‘re actually Unicode, 16-bit encoded binary information) Program • Binary Streams, containing 8-bit information (which is why they’re sometimes called byte-streams, as well)^ I^ ‘ M A S T R I N G \n Device

Program^0110100111101101 00000000 Device

Character and Byte Streams

Program reads^ source

a stream

Program writes^ dest

I/O in Java

  • Choose one of the abstract classes
  • Wrap functionality around it (buffers, files,

compression, etc.)

  • Format, if desired

java.text package – handles fonts, precision,

internationalism, etc.

Abstract I/O Classes

FileInputStream

FileOutputStream

byte-oriented I/O (8-bit) binary data machine-readable only

FileReader

FileWriter

char-oriented I/O (16-bit) text data human-readable

RandomAccess binary

key idea: layered objects! start with a stream & wrap other classes around it to add functionality

File Stream Objects

  • File stream:
    • One-way transmission path
    • Used to connect a file stored on a physical device

to a program

  • Mode
    • Determines direction of data on transmission path

File Stream Objects

  • Used to connect a file stored on a physical

device to a program

  • Input file stream:
    • File stream that reads data from a file into a

program

  • Output file stream:
    • File stream that writes data to a file
      • Can open associated file for:
        • Writing or Appending

Reading a File

  • Create input stream object:
    • FileReader fr = new FileReader("prices.txt");

fr.read()

Buffering

  • Every time a byte or sequence of bytes is

written to or read from a file:

  • Call to the underlying operating system is required
  • Resulting operating system calls can be

significantly reduced by buffering

  • Used for almost all file transfers
  • Buffered stream
  • Provides a storage area that is used by data as they

are transferred between a program and a file Docsity.com

Buffering (continued)

  • Disk device drivers only transfer data in

fixed sizes

  • Such as 1,024 bytes at a time
  • Buffered object streams:
  • Permit device drivers to transfer data in blocks

of one size, while the program accesses them

using a different size

  • Created from buffered stream classes
    • Always require the prior creation of a basic I/O

stream object Docsity.com