java input output
play

Java Input/Output 6 May 2019 OSU CSE 1 Overview The Java I/O - PowerPoint PPT Presentation

Java Input/Output 6 May 2019 OSU CSE 1 Overview The Java I/O (Input/Output) package java.io contains a group of interfaces and classes similar to the OSU CSE components SimpleReader and SimpleWriter component families Except that


  1. Java Input/Output 6 May 2019 OSU CSE 1

  2. Overview • The Java I/O (Input/Output) package java.io contains a group of interfaces and classes similar to the OSU CSE components’ SimpleReader and SimpleWriter component families – Except that java.io is far more general, configurable, and powerful (and messy) – Hence, the names SimpleReader and SimpleWriter 6 May 2019 OSU CSE 2

  3. I/O Streams • An input/output stream is a (conceptually not necessarily finite) series of data items – An input stream is a “flow” of data items from a source to a program • The program reads from the source (or from the stream) – An output stream is a “flow” of data items from a program to a destination • The program writes to the destination (or to the stream) 6 May 2019 OSU CSE 3

  4. Input Streams input stream program source single data item 6 May 2019 OSU CSE 4

  5. Input Streams input stream program source single data item Source may be the keyboard, a file on disk, a physical device, another program, even an array or String in the same program. 6 May 2019 OSU CSE 5

  6. Output Streams output stream program desti- nation single data item 6 May 2019 OSU CSE 6

  7. Output Streams output stream program desti- nation single data item Destination may be the console window, a file on disk, a physical device, another program, even an array or String in the same program. 6 May 2019 OSU CSE 7

  8. Part I: Beginner’s Guide • This part is essentially a “how-to” guide for using java.io that assumes knowledge of the OSU CSE components’ SimpleReader and SimpleWriter component families 6 May 2019 OSU CSE 8

  9. Keyboard Input ( SimpleReader ) • Here’s some code in main to read input from the keyboard, using SimpleReader : public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); String s = input.nextLine(); ... input.close(); } 6 May 2019 OSU CSE 9

  10. Keyboard Input ( SimpleReader ) Advice (except for the simplest programs): to guard against the user entering “unexpected” input, read a line • Here’s some code in main to read input at a time into a String and then parse it to see whether it looks like expected. from the keyboard, using SimpleReader : public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); String s = input.nextLine(); ... input.close(); } 6 May 2019 OSU CSE 10

  11. Overview of java.io Input Readable Closeable Reader InputStream- BufferedReader Reader There are more classes FileReader and interfaces not discussed here! 6 May 2019 OSU CSE 11

  12. Keyboard Input ( java.io ) • Here’s some code in main to read input from the keyboard, using java.io : public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 6 May 2019 OSU CSE 12

  13. Keyboard Input ( java.io ) Some methods in java.io throw exceptions (discussed later) under certain circumstances, and you either • Here’s some code in main to read input need to catch them or let them propagate “up the call chain”, like this. from the keyboard, using java.io : public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 6 May 2019 OSU CSE 13

  14. Keyboard Input ( java.io ) The variable System.in is a Java standard stream (discussed later), so you may use it without declaring it; but it • Here’s some code in main to read input is a byte stream (discussed later), so you want to wrap it ... from the keyboard, using java.io : public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 6 May 2019 OSU CSE 14

  15. Keyboard Input ( java.io ) ... in a character stream (discussed later) like this ... • Here’s some code in main to read input from the keyboard, using java.io : public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 6 May 2019 OSU CSE 15

  16. Keyboard Input ( java.io ) ... and then you want to wrap that character stream in a buffered stream • Here’s some code in main to read input (discussed later), like this, so ... from the keyboard, using java.io : public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 6 May 2019 OSU CSE 16

  17. Keyboard Input ( java.io ) ... you can read one line at a time into a String , like this. • Here’s some code in main to read input from the keyboard, using java.io : public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 6 May 2019 OSU CSE 17

  18. Keyboard Input ( java.io ) The declared types of variables when you use java.io are class types • Here’s some code in main to read input rather than interface types . from the keyboard, using java.io : public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 6 May 2019 OSU CSE 18

  19. Keyboard Input ( java.io )to This technique of slightly extending features or capabilities of an object by wrapping it inside another object is a • Here’s some code in main to read input popular object-oriented design pattern from the keyboard, using java.io : called the decorator pattern. public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); ... input.close(); } 6 May 2019 OSU CSE 19

  20. An Alternative ( java.util ) • An attractive alternative to using java.io.BufferedReader is to use java.util.Scanner – Important difference: no IOException s can be raised, so you don’t need to worry about catching or throwing them – Features for parsing input are powerful, e.g., the use of regular expressions to describe delimiters between data items 6 May 2019 OSU CSE 20

  21. An Alternative ( java.util ) • Here’s some code in main to read input from the keyboard, using Scanner : public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); ... input.close(); } 6 May 2019 OSU CSE 21

  22. An Alternative ( java.util ) Now, main does not declare that it throws IOException ; in this sense it is similar to using SimpleReader . • Here’s some code in main to read input from the keyboard, using Scanner : public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); ... input.close(); } 6 May 2019 OSU CSE 22

  23. An Alternative ( java.util ) Notice that initialization does not involve layers of wrapping of one object inside another; Scanner also has different • Here’s some code in main to read input constructors so it can be used with files. from the keyboard, using Scanner : public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); ... input.close(); } 6 May 2019 OSU CSE 23

  24. An Alternative ( java.util ) The method for reading a line into a String has a different name than for BufferedReader : it is nextLine (like • Here’s some code in main to read input SimpleReader ). from the keyboard, using Scanner : public static void main(String[] args) { Scanner input = new Scanner(System.in); String s = input.nextLine(); ... input.close(); } 6 May 2019 OSU CSE 24

  25. End-of-Stream ( SimpleReader ) public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); while (!input.atEOS()) { s = input.nextLine(); ... } ... input.close(); } 6 May 2019 OSU CSE 25

  26. End-of-Stream ( SimpleReader ) public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); while (!input.atEOS()) { s = input.nextLine(); ... } ... SimpleReader has a method to report “at input.close(); end-of-stream”, and it can tell you this before } you read “past the end of the input stream”; similarly, Scanner has method hasNext . 6 May 2019 OSU CSE 26

  27. End-of-Stream ( java.io ) public static void main(String[] args) throws IOException { BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); while (s != null ) { ... s = input.readLine(); } ... input.close(); } 6 May 2019 OSU CSE 27

  28. End-of-Stream ( java.io ) BufferedReader has no method to detect when you cannot read any more public static void main(String[] args) input, so you can check it only after you throws IOException { try to read “past the end of the stream”. BufferedReader input = new BufferedReader( new InputStreamReader(System.in)); String s = input.readLine(); while (s != null ) { ... s = input.readLine(); } ... input.close(); } 6 May 2019 OSU CSE 28

Recommend


More recommend