Java I/O

MELJUN CORTES

Streams, Readers, Writers, Buffers and
Files

MELJUN CORTES
What is a stream?
What is a stream?
 Webster’s Dictionary:


stream, n: [2b.] a constantly renewed
supply
What is a stream?
- Object that represents data being

delivered to or from an object.
What is a stream?
- Object that represents data being

delivered to or from an object.
- Behavior:
What is a stream?
- Object that represents data being

delivered to or from an object.
- Behavior: One byte at a time or o...
What is a stream?
-- code example:
What is a stream?
-- code example:
FileInputStream and FileOutputStream
What is a reader/writer?
What is a
reader/writer?
There are actually two kinds of streams:
What is a
reader/writer?
There are actually two kinds of streams:
 Byte Streams
What is a
reader/writer?
There are actually two kinds of streams:
 Byte Streams – read/write raw bytes
What is a
reader/writer?
There are actually two kinds of streams:
 Byte Streams – read/write raw bytes
 Character Stream...
What is a
reader/writer?
There are actually two kinds of streams:
 Byte Streams – read/write raw bytes
 Character Stream...
What is a reader/writer?
There are actually two kinds of streams:
 Byte Streams – read/write raw bytes
 Character Stream...
Two Kinds of Streams
Two Kinds of Streams
Hierarchies do not overlap:
Two Kinds of Streams
Hierarchies do not overlap:
Two Kinds of Streams
Two Kinds of Streams
Reader/Writer
-- Code example
Buffering
Buffering
 Accessing anything outside of memory

(disk, network, web…) is expensive.
Buffering
 Accessing anything outside of memory

(disk, network, web…) is expensive.
 Better to access a big lump at a t...
Buffering
-- Code example
LineNumberReader
LineNumberReader
 “A buffered character-input stream that

keeps track of line numbers.”
LineNumberReader
 “A buffered character-input stream that

keeps track of line numbers.”
 Code example 
LineNumberReader
 setLineNumber() does not seek to a line in

the file. It just designates the line number
of the first l...
LineNumberReader
 BufferedReader also has a readLine()

method. Use that if you don’t need to
keep track of line number.
InputStreamReader
InputStreamReader
 Converts bytes from an InputStream to

chars, using the proper encoding.
PrintWriter
PrintWriter
 System.out is a PrintWriter
PrintWriter
 “Print formatted representations of

objects to a text-output stream.”
PrintWriter
 “Print formatted representations of

objects to a text-output stream.”
 ex. print(boolean), print(char), pr...
PrintWriter
 “Print formatted representations of

objects to a text-output stream.”
 ex. print(boolean), print(char), pr...
PrintWriter
 “Print formatted representations of

objects to a text-output stream.”
 ex. print(boolean), print(char), pr...
Using Streams on the
Web
Using Streams on the
Web
Code examples:
Reading from a URL object.
 Reading from a URLConnection object.
 Writing to a U...
File
File
 Just a wrapper for a String, does not

necessarily denote something on the file
system.
File
 Just a wrapper for a String, does not

necessarily denote something on the file
system.
 ex: new File(“I don’t fri...
File
 Constructors
 Creating & deleting files
 Creating temp files and deleting on exit
 Moving, renaming and copying
...
File
Code example: Using FilenameFilter
Upcoming SlideShare
Loading in …5
×

MELJUN CORTES Java Lecture Input Output

263
-1

Published on

MELJUN CORTES Java Lecture Input Output

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
263
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

MELJUN CORTES Java Lecture Input Output

  1. 1. Java I/O MELJUN CORTES Streams, Readers, Writers, Buffers and Files MELJUN CORTES
  2. 2. What is a stream?
  3. 3. What is a stream?  Webster’s Dictionary:  stream, n: [2b.] a constantly renewed supply
  4. 4. What is a stream? - Object that represents data being delivered to or from an object.
  5. 5. What is a stream? - Object that represents data being delivered to or from an object. - Behavior:
  6. 6. What is a stream? - Object that represents data being delivered to or from an object. - Behavior: One byte at a time or one character at a time.
  7. 7. What is a stream? -- code example:
  8. 8. What is a stream? -- code example: FileInputStream and FileOutputStream
  9. 9. What is a reader/writer?
  10. 10. What is a reader/writer? There are actually two kinds of streams:
  11. 11. What is a reader/writer? There are actually two kinds of streams:  Byte Streams
  12. 12. What is a reader/writer? There are actually two kinds of streams:  Byte Streams – read/write raw bytes
  13. 13. What is a reader/writer? There are actually two kinds of streams:  Byte Streams – read/write raw bytes  Character Streams
  14. 14. What is a reader/writer? There are actually two kinds of streams:  Byte Streams – read/write raw bytes  Character Streams (“readers”, “writers”)
  15. 15. What is a reader/writer? There are actually two kinds of streams:  Byte Streams – read/write raw bytes  Character Streams (“readers”, “writers”) – read/write characters (16-bit unicode)
  16. 16. Two Kinds of Streams
  17. 17. Two Kinds of Streams Hierarchies do not overlap:
  18. 18. Two Kinds of Streams Hierarchies do not overlap:
  19. 19. Two Kinds of Streams
  20. 20. Two Kinds of Streams
  21. 21. Reader/Writer -- Code example
  22. 22. Buffering
  23. 23. Buffering  Accessing anything outside of memory (disk, network, web…) is expensive.
  24. 24. Buffering  Accessing anything outside of memory (disk, network, web…) is expensive.  Better to access a big lump at a time than one byte/char at a time.
  25. 25. Buffering -- Code example
  26. 26. LineNumberReader
  27. 27. LineNumberReader  “A buffered character-input stream that keeps track of line numbers.”
  28. 28. LineNumberReader  “A buffered character-input stream that keeps track of line numbers.”  Code example 
  29. 29. LineNumberReader  setLineNumber() does not seek to a line in the file. It just designates the line number of the first line.
  30. 30. LineNumberReader  BufferedReader also has a readLine() method. Use that if you don’t need to keep track of line number.
  31. 31. InputStreamReader
  32. 32. InputStreamReader  Converts bytes from an InputStream to chars, using the proper encoding.
  33. 33. PrintWriter
  34. 34. PrintWriter  System.out is a PrintWriter
  35. 35. PrintWriter  “Print formatted representations of objects to a text-output stream.”
  36. 36. PrintWriter  “Print formatted representations of objects to a text-output stream.”  ex. print(boolean), print(char), print(long), println(…
  37. 37. PrintWriter  “Print formatted representations of objects to a text-output stream.”  ex. print(boolean), print(char), print(long), println(…  Auto-flush on newline (default behavior).
  38. 38. PrintWriter  “Print formatted representations of objects to a text-output stream.”  ex. print(boolean), print(char), print(long), println(…  Auto-flush on newline (default behavior).  Can wrap a Writer or an OuputStream.
  39. 39. Using Streams on the Web
  40. 40. Using Streams on the Web Code examples: Reading from a URL object.  Reading from a URLConnection object.  Writing to a URLConnection object  Writing to a HttpServletResponse object. 
  41. 41. File
  42. 42. File  Just a wrapper for a String, does not necessarily denote something on the file system.
  43. 43. File  Just a wrapper for a String, does not necessarily denote something on the file system.  ex: new File(“I don’t friggin’ care if this is a valid filename.”); will compile.
  44. 44. File  Constructors  Creating & deleting files  Creating temp files and deleting on exit  Moving, renaming and copying  Other useful File methods  Performance issue with regards to using File methods
  45. 45. File Code example: Using FilenameFilter
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×