Lecture 27/28 - IO Streams Reader/ Deeper Look Into Filed

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards

what are the I/O stream

  • most input/output is based on I/O streams

  • objects that support I/O commands

  • System.out for standard output

  • System.in for standard input

2
New cards

Two Categories o f data

  • Machine formatted data

    • binary form, 0101000101001, each binary digit takes on bit

  • Human readable text

    • represented by characters

  • To view machine formatted data as human readable txt, an interpreter is required,

    • Microsoft word is an interpreter for word documents

3
New cards

Byte Streams

  • for the I/O of machine formatted data

  • class name ends with stream

  • input stream and output stream should be used in pairs

4
New cards

Character Streams

  • for the I/O of human readable text

  • class names ends with Reader or Writer

  • Reader and writer should be used in pairs

5
New cards

Example of typing into screen

  • goes to keyboard

  • System.in; Input Stream

  • Goes to the Memory.

  • System.out; Output Stream

<ul><li><p>goes to keyboard</p></li><li><p>System.in; Input Stream</p></li><li><p>Goes to the Memory.</p></li><li><p>System.out; Output Stream</p></li></ul><p></p>
6
New cards

Reading from text file example

  • Memory, 1’s and 0’s

  • output stream to text file

  • text file; input stream to 1’s and 0’s

<ul><li><p>Memory, 1’s and 0’s</p></li><li><p>output stream to text file</p></li><li><p>text file; input stream to 1’s and 0’s</p></li></ul><p></p>
7
New cards

Main classes of character stream

  • Reader and Writer

  • All other character classes are subclasses of Reader and Writer

    • FileReader, BufferedReader, FileWriter, BufferedWriter

    • all support same ser of basic operations defined by Reader or Writer

8
New cards

Reader class, goal example

  • read a value from a character stream into a variable in java

  • translate a value in character form into machine formatted data

  • read 3.1415926 from character stream (9 bytes) , translate and assign to a machine formatted float var (4 bytes)

9
New cards

Writer, goal example

  • write a value from a variable in Java into a character stream

  • translate a machine formatted data into a character stream

  • translate machine formatted float variable, pi, into a character stream, and write into output device

10
New cards

Main Classes of Byte Stream

  • InputStream and Output Stream

  • all other Byte Stream are subclasses of InputStream and OutputStream

    • FileInputStream, BufferedInputStream, FileOutputStream BufferedOutputStream

  • Doesn’t have any basic operations that involve translation

  • always machine-formatted data

    • difficult for human to inspect what’s going on

    • efficient in time and space

11
New cards

How Should I decide witch to use

  • Byte Streams are useful and more efficient for direct machine to machine communication; also efficient in storing data and reading data from files

  • Character Streams; are useful when dealing with sequence of characters and when humans want to inspect the data

  • as a rule of thumb; if long series of ones and zeroes you need to know what info it is meant to represent and how the info is encoded before you will be ablet o interpret it

12
New cards

Java Reader

  • an abstract class for reading character streams

  • subclasses must implement read and close()

  • we use it’s subclasses

    • BufferedReader: wrapper for InputStreamReader, buffers bytes each time a native I/O is called; more efficent because it frequently accesses RAM instead of hard drive

    • InputStreamReader: reads bytes and decodes them into characters using a specified charset,

13
New cards

Note on the InputStreamReader class

  • reads chaarcter from any kind of input stream

  • one has to open a file as a byte stream before reading by InputStreamReader

14
New cards

Analogy of Stream, Reader and Writer with milk

knowt flashcard image
15
New cards

FileWriter Class

  • write character directly into files

  • no need to open file as a byte stream first and then convert it as a character stream

16
New cards

OutputStreamWriter

  • write characters into any kind of output stream such as FileInputStream,

  • we should open files as one kind of character stream and then use OutputStreamWriter to write into it

17
New cards

Buffered I/O explained

  • a buffer collects large amount of data, and does I/O in batches to reduce the number of times to invoke and interrupt a disk drive

  • only write to hard drive when buffer is full

  • flush(); flushes the output stream and forces any buffered output byte to be written out;

18
New cards

path name and directory

  • location of file in file system

  • name of directory; name of file;

  • abs path; path starting from root

  • relative; current folder

19
New cards

Issue with Path In different OS

  • Path are formatted slightly different on different OS

  • The Class File is an abstraction of the location of files

    • it hides different detailed formats of file path in diff OS

    • unifies diff file path and directory operation in diff OS as the same set of basic operations

20
New cards