1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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
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
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
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
Example of typing into screen
goes to keyboard
System.in; Input Stream
Goes to the Memory.
System.out; Output Stream
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
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
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)
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
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
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
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,
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
Analogy of Stream, Reader and Writer with milk
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
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
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;
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
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