Upload
meljun-cortes
View
74
Download
2
Embed Size (px)
Citation preview
Java I/OJava I/O
Streams, Readers, Writers, Buffers and Streams, Readers, Writers, Buffers and FilesFiles
MELJUN CORTES, MBA,MPA,BSCSMELJUN CORTES, MBA,MPA,BSCS
What is a stream?What is a stream?
Webster’s Dictionary: Webster’s Dictionary: stream, n: [2b.] a constantly renewed stream, n: [2b.] a constantly renewed
supply supply
What is a stream?What is a stream?
- Object that represents data being Object that represents data being delivered to or from an object.delivered to or from an object.
What is a stream?What is a stream?
- Object that represents data being Object that represents data being delivered to or from an object.delivered to or from an object.
- Behavior:Behavior:
What is a stream?What is a stream?
- Object that represents data being Object that represents data being delivered to or from an object.delivered to or from an object.
- Behavior: One byte at a time or one Behavior: One byte at a time or one character at a time.character at a time.
What is a stream?What is a stream?
-- code example: -- code example:
FileInputStream and FileOutputStreamFileInputStream and FileOutputStream
What is a What is a reader/writer?reader/writer?There are actually two kinds of streams:There are actually two kinds of streams:
What is a What is a reader/writer?reader/writer?There are actually two kinds of streams:There are actually two kinds of streams: Byte StreamsByte Streams
What is a What is a reader/writer?reader/writer?There are actually two kinds of streams:There are actually two kinds of streams: Byte Streams – read/write raw bytesByte Streams – read/write raw bytes
What is a What is a reader/writer?reader/writer?
There are actually two kinds of streams:There are actually two kinds of streams: Byte Streams – read/write raw bytesByte Streams – read/write raw bytes Character StreamsCharacter Streams
What is a What is a reader/writer?reader/writer?There are actually two kinds of streams:There are actually two kinds of streams: Byte Streams – read/write raw bytesByte Streams – read/write raw bytes Character Streams (“readers”, “writers”)Character Streams (“readers”, “writers”)
What is a reader/writer?What is a reader/writer?
There are actually two kinds of streams:There are actually two kinds of streams: Byte Streams – read/write raw bytesByte Streams – read/write raw bytes Character Streams (“readers”, “writers”) – Character Streams (“readers”, “writers”) –
read/write characters (16-bit unicode)read/write characters (16-bit unicode)
BufferingBuffering
Accessing anything outside of memory Accessing anything outside of memory (disk, network, web…) is expensive.(disk, network, web…) is expensive.
BufferingBuffering
Accessing anything outside of memory Accessing anything outside of memory (disk, network, web…) is expensive.(disk, network, web…) is expensive.
Better to access a big lump at a time than Better to access a big lump at a time than one byte/char at a time.one byte/char at a time.
LineNumberReaderLineNumberReader
““A buffered character-input stream that A buffered character-input stream that keeps track of line numbers.”keeps track of line numbers.”
LineNumberReaderLineNumberReader
““A buffered character-input stream that A buffered character-input stream that keeps track of line numbers.”keeps track of line numbers.”
Code example Code example
LineNumberReaderLineNumberReader
setLineNumber() does not seek to a line in setLineNumber() does not seek to a line in the file. It just designates the line number the file. It just designates the line number of the first line.of the first line.
LineNumberReaderLineNumberReader
BufferedReader also has a readLine() BufferedReader also has a readLine() method. Use that if you don’t need to method. Use that if you don’t need to keep track of line number.keep track of line number.
InputStreamReaderInputStreamReader
Converts bytes from an InputStream to Converts bytes from an InputStream to chars, using the proper encoding.chars, using the proper encoding.
PrintWriterPrintWriter
““Print formatted representations of Print formatted representations of objects to a text-output stream.”objects to a text-output stream.”
PrintWriterPrintWriter
““Print formatted representations of Print formatted representations of objects to a text-output stream.”objects to a text-output stream.”
ex. print(boolean), print(char), print(long), ex. print(boolean), print(char), print(long), println(…println(…
PrintWriterPrintWriter
““Print formatted representations of Print formatted representations of objects to a text-output stream.”objects to a text-output stream.”
ex. print(boolean), print(char), print(long), ex. print(boolean), print(char), print(long), println(…println(…
Auto-flush on newline (default behavior).Auto-flush on newline (default behavior).
PrintWriterPrintWriter
““Print formatted representations of Print formatted representations of objects to a text-output stream.”objects to a text-output stream.”
ex. print(boolean), print(char), print(long), ex. print(boolean), print(char), print(long), println(…println(…
Auto-flush on newline (default behavior).Auto-flush on newline (default behavior). Can wrap a Writer or an OuputStream.Can wrap a Writer or an OuputStream.
Using Streams on the Using Streams on the WebWeb
Code examples:Code examples: Reading from a URL object.Reading from a URL object. Reading from a URLConnection object.Reading from a URLConnection object. Writing to a URLConnection objectWriting to a URLConnection object Writing to a HttpServletResponse object.Writing to a HttpServletResponse object.
FileFile
Just a wrapper for a String, does not Just a wrapper for a String, does not necessarily denote something on the file necessarily denote something on the file system.system.
FileFile
Just a wrapper for a String, does not Just a wrapper for a String, does not necessarily denote something on the file necessarily denote something on the file system.system.
ex: new File(“I don’t friggin’ care if this is ex: new File(“I don’t friggin’ care if this is a valid filename.”); will compile.a valid filename.”); will compile.
FileFile
ConstructorsConstructors Creating & deleting filesCreating & deleting files Creating temp files and deleting on exitCreating temp files and deleting on exit Moving, renaming and copyingMoving, renaming and copying Other useful File methodsOther useful File methods Performance issue with regards to using Performance issue with regards to using
File methodsFile methods