Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE205 Lecture 24

CSE205 Lecture 24

Object-Oriented Programming and Data Structures
Serialization
(202111)

Javier Gonzalez-Sanchez
PRO

October 04, 2021
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs
    CSE 205
    Object-Oriented Programming and
    Data Structures
    Lecture 24: Streams and Exceptions
    Dr. Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    PERALTA 230U
    Office Hours: By appointment

    View Slide

  2. jgs
    Streams

    View Slide

  3. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 3
    jgs
    Where we are …
    Data Structures
    Files

    View Slide

  4. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 4
    jgs
    I/O Streams
    § An I/O Stream represents an input source or an output destination

    View Slide

  5. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 5
    jgs
    Hierarchy

    View Slide

  6. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 6
    jgs
    File Stream (binary data)
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    public class CopyBytes {
    public static void main(String[] args) {
    try {
    FileInputStream in = new FileInputStream(”myfile.txt");
    FileOutputStream out =new FileOutputStream("outagain.txt");
    int c;
    while ((c = in.read()) != -1) {
    out.write(c);
    }
    in.close();
    out.close();
    } catch (IOException e) {
    System.out.println(“Failure!”);
    }
    }
    }

    View Slide

  7. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 7
    jgs
    File Stream (char data)
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    public class CopyCharacters {
    public static void main(String[] args) {
    try {
    FileReader inputStream = new FileReader(”myFile.txt");
    FileWriter outputStream = new FileWriter(”output.txt");
    int c;
    while ((c = inputStream.read()) != -1) {
    outputStream.write(c);
    }
    inputStream.close();
    outputStream.close();
    } catch (IOException e) {
    System.out.println(“Failure!”);
    }
    }
    }

    View Slide

  8. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 8
    jgs
    File Stream (string data)
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.BufferedReader;
    import java.io.PrintWriter;
    import java.io.IOException;
    public class CopyLines {
    public static void main(String[] args) {
    try {
    FileReader input = new FileReader(”myFile.txt");
    FileWriter output = new FileWriter(”output.txt");
    BufferedReader inputStream = new BufferedReader(input);
    PrintWriter outputStream = new PrintWriter(output);
    String l;
    while ((l = inputStream.readLine()) != null) {
    outputStream.println(l);
    }
    inputStream.close();
    outputStream.close();
    } catch (IOException e) {
    System.out.println(“Failure!”);
    }
    }
    }

    View Slide

  9. jgs
    Exceptions
    try – catch statement

    View Slide

  10. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 10
    jgs
    Exceptions
    § When executing Java code, different errors can occur due to wrong input, or
    other unforeseeable things.
    § When an error occurs, Java will normally stop and generate an error
    message.
    The technical term for this is: Java will throw an exception (an error).
    § The try statement allows you to define a block of code to be tested for
    errors while it is being executed.
    § The catch statement allows you to define a block of code to be executed, if
    an error occurs in the try block.
    § The try and catch keywords come in pairs.

    View Slide

  11. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 11
    jgs
    try-catch Statements
    try {
    // Block of code to try
    } catch(Exception e) {
    // Block of code to handle errors
    }

    View Slide

  12. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 12
    jgs
    Example

    View Slide

  13. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 13
    jgs
    Exceptions
    There are many exception types available in Java:
    § ArithmeticException,
    § FileNotFoundException,
    § ArrayIndexOutOfBoundsException,
    § SecurityException,
    § etc.
    § The parent of all is the class Exception

    View Slide

  14. jgs

    View Slide

  15. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 15
    jgs
    Example

    View Slide

  16. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 16
    jgs
    finally Statement
    § The finally statement lets you execute code, after try...catch,
    regardless of the result

    View Slide

  17. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 17
    jgs
    Example

    View Slide

  18. jgs
    throw

    View Slide

  19. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 19
    jgs
    Definition
    § The throw statement allows you to create a custom error

    View Slide

  20. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 20
    jgs
    Example

    View Slide

  21. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 21
    jgs
    Example

    View Slide

  22. Javier Gonzalez-Sanchez | CSE205 | Fall 2021 | 22
    jgs
    Questions

    View Slide

  23. jgs
    CSE 205 Object-Oriented Programming and Data Structures
    Javier Gonzalez-Sanchez, Ph.D.
    [email protected]
    Fall 2021
    Copyright. These slides can only be used as study material for the class CSE205 at Arizona State University.
    They cannot be distributed or used for another purpose.

    View Slide