
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 java.io is far more general, configurable, and powerful (and messy) – Hence, the names SimpleReader and SimpleWriter 6 May 2019 OSU CSE 2 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 Input Streams input stream program source single data item 6 May 2019 OSU CSE 4 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 Output Streams program output stream desti- nation single data item 6 May 2019 OSU CSE 6 Output Streams program output stream 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 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 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 KeyboardAdvice (except Input for the (simplest ) programs): to guard againstSimpleReader the user entering “unexpected” input, read a line • atHere’s a time into some a String codeand in thenmain parseto read input 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 Overview of java.io Input Readable Closeable Reader InputStream- BufferedReader Reader FileReader There are more classes and interfaces not discussed here! 6 May 2019 OSU CSE 11 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 SomeKeyboard methods in java.io Inputthrow ( ) exceptions (discussed later) underjava.io certain circumstances, and you either • Here’sneed tosome catch codethem or in let main them to read input 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 TheKeyboard variable System.in Inputis a Java( ) standard stream (discussed later),java.io so you may use it without declaring it; but it • isHere’s a byte streamsome (discussedcode in main later), soto read input 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 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 Keyboard Input ( ) ... and then you want to wrap thatjava.io character stream in a buffered stream • Here’s(discussed some later), code like this,in main so ... 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 16 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 Keyboard Input ( ) The declared typesjava.io of variables when you use java.io are class types • Here’s some coderather in mainthan interfaceto read types 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 18 KeyboardThis Input technique ( of slightly extending)to features or capabilitiesjava.io of an object by wrapping it inside another object is a • Here’s somepopular code objectin main-orientedto read design input pattern from the keyboard,called using the decorator java.iopattern.: 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 An Alternative (java.util) • An attractive alternative to using java.io.BufferedReader is to use java.util.Scanner – Important difference: no IOExceptions 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 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 AnNow, mainAlternativedoes not declare (java.util 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 NoticeAn that Alternative initialization does not(java.util involve ) layers of wrapping of one object inside another; Scanner also has different • constructorsHere’s some so it can code be used in main with files.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 23 TheAn method Alternative for reading a line(java.util into a ) String has a different name than for • BufferedReaderHere’s some code: it is nextLine in (liketo read input SimpleReader). main 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 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 End-of-Stream (SimpleReader) public static void main(String[] args) { SimpleReader input = new SimpleReader1L(); while (!input.atEOS()) { s = input.nextLine(); ... } ... has a method to report “at input.closeSimpleReader(); 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 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 End-of-Stream ( ) BufferedReaderjava.iohas no method to detect when you cannot read any more public static input,void main(String[]so you can check args it only) after you throws IOExceptiontry 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 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) { ... Before checking again, s = input.readLine(); you must try to read } another line. ... input.close(); } 6 May 2019 OSU CSE 29 File Input (SimpleReader) • Here’s some code in main to read input from a file, using SimpleReader: public static void main(String[] args) { SimpleReader input = new SimpleReader1L("data/test.txt"); String s = input.nextLine(); ... input.close(); } 6 May 2019 OSU CSE 30 File Input ( ) SimpleReader has aSimpleReader constructor from a String, which is the name of the file • Here’syou some
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages69 Page
-
File Size-