Please enable JavaScript.
Coggle requires JavaScript to display documents.
Chapter 08. IO (Working with Streams (The BufferedReader and…
Chapter 08. IO
Working with Streams
The ObjectInputStream and ObjectOutputStream Classes
The
Serializable
Interface
tagging interface to serialize objects
transient
keyword to
skip
serialize and
lost
data
static
class members will be ignored
use
serialVersionUID
to identify
uniquely
a version of the class and NOT
required
The PrintStream and PrintWriter Classes
The BufferedInputStream and BufferedOutputStream
public void copy(
File
source,
File
destination) throws IOException {
try (InputStream in = new
BufferedInputStream
(new
FileInputStream
(source));
OutputStream out = new
BufferedOutputStream
(new
FileOutputStream
(destination))){
byte[] buffer = new byte[1024];
int lengthRead;
while((
lengthRead
= in.
read
(
buffer
)) > 0){
out.
write
(buffer,0,lengthRead);
out.flush();
}
}
}
Review of Stream Classes
The FileInputStream and FileOutputStream Classes
public void copy(
File
source,
File
destination) throws IOException {
try (InputStream in = new
FileInputStream
(source);
OutputStream out = new
FileOutputStream
(destination)){
int b;
while((b = in.
read
()) != -1){
out.
write
(b)
}
}
}
The FileReader and FileWriter
The BufferedReader and BufferedWriter Classes
public List<String> readFile(
File
source) throws IOException {
List<String> data = new ArrayList<String>();
try (BufferedReader reader = new
BufferedReader
(new
FileReader
(source))){
String s;
while((s = reader.
readLine
()) != null){
data.add(s);
}
}
}
public void writeFile(List<String> data, File destination) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(destination))){
for (String s: data){
writer.write(s);
writer.newLine();
}
}
}
Introducing Streams
Stream Nomenclature
Low-Level vs.
High-Level Streams
Low-level: connect directly with the source of the data
High-level: built on top of another stream using wrapping
Example
try(
BufferedReader br = new BufferedReader(new FileReader("zoo-data.txt"))){
System.out.println(br.readLine());
}
Stream Base Classes
(4 abstract classes)
InputStream
Low
FileInputStream
High
ObjectInputStream
OutputStream
Low
FileOutputStream
High
ObjectOutputStream
PrintStream
Reader
Low
FileReader
High
BufferedReader
InputStreamReader
Writer
Low
FileWriter
High
BufferedWriter
OutputStreamWriter
PrintWriter
Input and Output
Input / Output
Reader/Writer
Decoding Java I/O class Names
Input
and
Output
Low-level
and
High-level
stream
Buffered
in its name reads or writes data in groups of bytes or characters
Byte Streams vs. Character Streams
Streams
I/O all types of binary or byte data
Readers/Writers
I/O only character and
String
data
Common Stream Operations
Flushing the Stream
flush() : request that all accumulated data be written immediately to disk
close() method automatically call the flush()
Marking the Stream
include in
InputStream
and
Reader
move the stream back to an earlier position
mark
(int)
read-ahead limit value (int bytes)
reset
()
revert to an earlier state
markSupported(): check if mark() is supported.
Closing the Stream
Fail to close a file properly could leave it locked by the OS
Close stream resources using the try-with-resource syntax
Skipping over Data
skip
(long): skip over a certain number of bytes
Stream Fundamentals
Understanding Files and Directories
Conceptualizing the File System
Introducing the File Class
Create a File Object
Address types
Absolute path
Relative path
Examples
File parent = new File("/home/smith");
File child = new File(parent,"data/zoo.txt");
Working with a File Object
lastModified()
delete()
length()
renameTo(File)
isFile()
mkdir()
isDirectory()
mkdirs()
getAbsolutePath()
getParent()
getName()
listFiles()
exists()
Interacting with Users
The Old Way
The New Way