MELJUN CORTES JAVA_IO

Embed Size (px)

Citation preview

  • 8/10/2019 MELJUN CORTES JAVA_IO

    1/45

    Java I/O

    Streams, Readers, Writers, Buffers andFiles

    MELJUN CORTES MBA MPA BSCS

  • 8/10/2019 MELJUN CORTES JAVA_IO

    2/45

    What is a stream?

  • 8/10/2019 MELJUN CORTES JAVA_IO

    3/45

    What is a stream?

    Websters Dictionary:stream, n: [2b.] a constantly renewedsupply

  • 8/10/2019 MELJUN CORTES JAVA_IO

    4/45

    What is a stream?

    - Object that represents data beingdelivered to or from an object.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    5/45

  • 8/10/2019 MELJUN CORTES JAVA_IO

    6/45

    What is a stream?

    - Object that represents data beingdelivered to or from an object.

    - Behavior: One byte at a time or onecharacter at a time.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    7/45

    What is a stream?

    -- code example:

  • 8/10/2019 MELJUN CORTES JAVA_IO

    8/45

  • 8/10/2019 MELJUN CORTES JAVA_IO

    9/45

    What is a reader/writer?

  • 8/10/2019 MELJUN CORTES JAVA_IO

    10/45

    What is a reader/writer?

    There are actually two kinds of streams:

  • 8/10/2019 MELJUN CORTES JAVA_IO

    11/45

    What is a reader/writer?

    There are actually two kinds of streams:Byte Streams

  • 8/10/2019 MELJUN CORTES JAVA_IO

    12/45

    What is a reader/writer?

    There are actually two kinds of streams:Byte Streams read/write raw bytes

  • 8/10/2019 MELJUN CORTES JAVA_IO

    13/45

  • 8/10/2019 MELJUN CORTES JAVA_IO

    14/45

    What is a reader/writer?

    There are actually two kinds of streams:Byte Streams read/write raw bytesCharacter Streams (readers, writers)

  • 8/10/2019 MELJUN CORTES JAVA_IO

    15/45

    What is a reader/writer?

    There are actually two kinds of streams:Byte Streams read/write raw bytesCharacter Streams (readers, writers) read/write characters (16-bit unicode)

  • 8/10/2019 MELJUN CORTES JAVA_IO

    16/45

    Two Kinds of Streams

  • 8/10/2019 MELJUN CORTES JAVA_IO

    17/45

    Two Kinds of Streams

    Hierarchies do not overlap:

  • 8/10/2019 MELJUN CORTES JAVA_IO

    18/45

    Two Kinds of Streams

    Hierarchies do not overlap:

  • 8/10/2019 MELJUN CORTES JAVA_IO

    19/45

    Two Kinds of Streams

  • 8/10/2019 MELJUN CORTES JAVA_IO

    20/45

    Two Kinds of Streams

  • 8/10/2019 MELJUN CORTES JAVA_IO

    21/45

  • 8/10/2019 MELJUN CORTES JAVA_IO

    22/45

    Buffering

  • 8/10/2019 MELJUN CORTES JAVA_IO

    23/45

    Buffering

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

  • 8/10/2019 MELJUN CORTES JAVA_IO

    24/45

    Buffering

    Accessing anything outside of memory(disk, network, web) is expensive. Better to access a big lump at a time thanone byte/char at a time.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    25/45

    Buffering

    -- Code example

  • 8/10/2019 MELJUN CORTES JAVA_IO

    26/45

    LineNumberReader

  • 8/10/2019 MELJUN CORTES JAVA_IO

    27/45

    LineNumberReader

    A buffered character -input stream thatkeeps track of line numbers.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    28/45

  • 8/10/2019 MELJUN CORTES JAVA_IO

    29/45

    LineNumberReader

    setLineNumber() does not seek to a line inthe file. It just designates the line numberof the first line.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    30/45

    LineNumberReader

    BufferedReader also has a readLine()method. Use that if you dont need tokeep track of line number.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    31/45

    InputStreamReader

  • 8/10/2019 MELJUN CORTES JAVA_IO

    32/45

    InputStreamReader

    Converts bytes from an InputStream tochars, using the proper encoding.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    33/45

  • 8/10/2019 MELJUN CORTES JAVA_IO

    34/45

    PrintWriter

    System.out is a PrintWriter

  • 8/10/2019 MELJUN CORTES JAVA_IO

    35/45

    PrintWriter

    Print formatted representations ofobjects to a text- output stream.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    36/45

  • 8/10/2019 MELJUN CORTES JAVA_IO

    37/45

    PrintWriter

    Print formatted representations ofobjects to a text- output stream. ex. print(boolean), print(char), print(long),println( Auto-flush on newline (default behavior).

  • 8/10/2019 MELJUN CORTES JAVA_IO

    38/45

    PrintWriter

    Print formatted representations ofobjects 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.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    39/45

    Using Streams on the Web

  • 8/10/2019 MELJUN CORTES JAVA_IO

    40/45

    Using Streams on the Web

    Code examples:Reading from a URL object.Reading from a URLConnection object.Writing to a URLConnection objectWriting to a HttpServletResponse object.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    41/45

    File

  • 8/10/2019 MELJUN CORTES JAVA_IO

    42/45

    File

    Just a wrapper for a String, does notnecessarily denote something on the filesystem.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    43/45

    File

    Just a wrapper for a String, does notnecessarily denote something on the filesystem.ex: new File(I dont friggin care if this isa valid filename.); will compile.

  • 8/10/2019 MELJUN CORTES JAVA_IO

    44/45

    File

    ConstructorsCreating & deleting filesCreating temp files and deleting on exitMoving, renaming and copyingOther useful File methodsPerformance issue with regards to usingFile methods

  • 8/10/2019 MELJUN CORTES JAVA_IO

    45/45

    File

    Code example: Using FilenameFilter