An I/O Stream represents an input source or an output destination.
- Perform input and output of 8-bit bytes
- All byte stream classes are descended from
InputStream
andOutputStream
. - ALWAYS close streams by invoking
close()
in finally block - ALL other stream types are built on byte streams.
- Input and output int (holds a
byte
value) withread()
. - Input and output byte array.
- Return -1 when reach the end of file.
- In addition to byte stream, character stream automatically translates to and from the local character set.
- Java uses Unicode convention to store characters.
- Usually, 8-bit superset of ASCII
- All character stream classes are descended from
Reader
andWriter
.
- Input and output int (hold a
character
value) withread()
. - Input and output character array or String.
- Return -1 when reach the end of file.
- Read data from buffer, then native input API is called only when the buffer is empty.
- Write data to buffer, then native output API is called only when the buffer is full
- Buffered stream classes:
- BufferedInputStream -> FileInputStream
- BufferedOutputStream -> FileOutputStream
- BufferedReader -> FileReader
- BufferedWriter -> FileWriter
(created by wrapping unbuffered streams)
Flushing the buffer: write out without waiting for it to fill.
flush()
is valid on any output stream, but has no effect unless the stream is buffered.
- Formatted input -> tokens -> corresponding data type
- Default delimiter white space including blanks, tabs, and line terminators.
- Set delimiter: 'useDelimiter(regex)'.
hasNext()
checks EOFnext()
returns String- Set locale:
useLocale(Locale.<location>)
.
Stream objects that implement formatting are instances of either PrintWriter
(character stream) or PrintStream
(byte stream).
note: The only PrintStream
objects we are likely to need are System.out
and System.err
.
print()
&println()
: standard wayformat()
: based on a format string.
- Standard Input:
System.in
- Standard Error:
System.err
- Standard Output:
System.out
- All standard streams are byte streams.
System.out
&System.err
->PrintStream
which utilizes an internal character stream object to emulate many of the features of character streams.System.in
has no character stream feature. But we can wrap it inInputStreamReader
.
- Useful for secure password entry.
- Provides both input and output streams that are true character streams.
- Invoking
System.console()
to retrieveConsole
object if it is available. readPassword()
returns char array.
- Support binary I/O of primitive data type values as well as String values.
DataInputStream
->DataInput
.DataOutputStream
->DataOutput
.- ReturnType read[Type]/write[Type] ().
- Throws
EOFException
when reach of EOF. - Don't use floating point numbers to represent monetary values because of bad decimal fractions. We should use
Object Stream
&BigDecimal
.
- Support I/O of objects.
ObjectInputStream
->ObjectInput
->DataInput
ObjectOutputStream
->ObjectOutput
->DataOutput
- Complex object writes all its referenced objects to stream.
- Two objects -> single object on the same stream => Two objects -> single object when read back.
- Reflects underlying platform.
- Creation:
Paths.get(<path>)
<=>FileSystems.getDefault().getPath(<path>)
.Paths.get(<dir1>, <dir2>, <fileName>)
- Path stores name elements as a sequence:
/home/user/foo
=>[home, user, foo]
.
getName(index)
getNameCount
: number of elements in the pathsubpath(beginIndex, endIndex)
getParent()
: elements[0:n-2]getRoot()
: if it is relative path, it returns null.
- Converting a Path:
toUri()
toAbsolutePath()
toRealPath()
- if true is passed to this method and the file system supports symbolic links => resolves symbolic links.
- relative path => absolute path
- remove redundant elements.
- Joining Two Paths:
resolve(<relativePath>)
. - Creating a path between two paths:
p1.relativize(p2)
. - Comparing two paths:
equals()
startsWith(<path>)
endsWith(<path>)
- Checking existence:
- verified to exist:
exists(Path, LinkOption...)
- verified to not exist:
notExists(Path, LinkOption...)
- status is unknown.
- Checking Accessibility:
isReadable(Path)
isWritable(Path)
isExecutable(Path)
- Checking whether two paths locate the same file.
- If deleting symbolic link, only the link is deleted.
delete(Path)
: throws an exception if the deletion fails.deleteIfExists(Path)
: no exception is thrown.
copy(sourcePath, targetPath, CopyOptions...)
copy(InputStream, Path, CopyOptions...)
: copy all bytes from an input stream to a file.copy(Path, OutputStream)
: copy all bytes from a file to an output stream.
move(sourcePath, targetPath, CopyOptions...)