32.Java-Input-Output - Java Input/Output 11 April 2013 OSU...

Info icon This preview shows pages 1–14. Sign up to view the full content.

Java Input/Output 11 April 2013 OSU CSE 1
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

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 11 April 2013 OSU CSE 2
Image of page 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) 11 April 2013 OSU CSE 3
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Input Streams 11 April 2013 OSU CSE 4 source input stream program single data item
Image of page 4
Input Streams 11 April 2013 OSU CSE 5 source input stream program 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.
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

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

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

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 11 April 2013 OSU CSE 8
Image of page 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(); } 11 April 2013 OSU CSE 9
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

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(); } 11 April 2013 OSU CSE 10 Advice (except for the simplest programs): to guard against the user entering “unexpected” input, read a line at a time into a String and then parse it to see whether it looks like expected.
Image of page 10
Overview of java.io Input 11 April 2013 OSU CSE 11 InputStream- Reader Readable Reader Closeable FileReader BufferedReader There are more classes and interfaces not discussed here!
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

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(); } 11 April 2013 OSU CSE 12
Image of page 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(); ...
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

Image of page 14
This is the end of the preview. Sign up to access the rest of the document.
  • Spring '15
  • WayneHeym
  • stream, Input/output, Standard streams

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern