InputOutput In Java input and output take place



![Example of Input class Exampleof. Input{ public static void main(String arg[]){ Console. Reader console Example of Input class Exampleof. Input{ public static void main(String arg[]){ Console. Reader console](https://slidetodoc.com/presentation_image_h2/413eba7a2f0668bcda406be133df160a/image-4.jpg)

- Slides: 5

Input/Output • In Java input and output take place through I/O streams – An I/O stream represents an input source or output destination – Streams support various data types – A program uses an input stream to read data from some input device – A program uses an output stream to send data to some output device

Command line I/O • System. in is a raw byte stream – Also known as the standard input stream – Not too useful • System. out is an output stream – We have already used it for printing

Console I/O Reader import java. io. Buffered. Reader; import java. io. Input. Stream. Reader; import java. io. IOException; public class Console. Reader{ public Console. Reader(Input. Stream in. Stream){ reader = new Buffered. Reader (new Input. Stream. Reader(in. Stream)); } public int read. Int() { String input. String = read. Line(); int n = Integer. parse. Int(input. String); return n; } public double read. Double() { String input. String = read. Line(); double x = Double. parse. Double(input. String); return x; } public char read. Char(){ String input. String = read. Line(); char x = input. String. char. At(0); return x; } public String read. Line() { String input. Line = ""; try { input. Line=reader. read. Line(); } catch (IOException e) { System. out. println(e); System. exit(1); } return input. Line; } private Buffered. Reader reader; }
![Example of Input class Exampleof Input public static void mainString arg Console Reader console Example of Input class Exampleof. Input{ public static void main(String arg[]){ Console. Reader console](https://slidetodoc.com/presentation_image_h2/413eba7a2f0668bcda406be133df160a/image-4.jpg)
Example of Input class Exampleof. Input{ public static void main(String arg[]){ Console. Reader console = new Console. Reader(System. in); System. out. println("Pls give integer input"); int n = console. read. Int(); System. out. println("Your input was "+n+'n'); System. out. println("Pls give float input"); double x = console. read. Double(); System. out. println("Your input was "+x+'n'); System. out. println("Pls give character input"); char y = console. read. Char(); System. out. println("Your input was "+y+'n');

Factorial I/O /* Find factorial of a number */ class factio { public static void main (String ars[]) { Console. Reader console = new Console. Reader(System. in); int i, n; int factorial; n=1; while (n>-1){ System. out. println("n. Give integer input"); n = console. read. Int(); factorial=1; for (i=1; i<=n; i=i+1) factorial=factorial*i; System. out. println("Factorial of "+ n +" is "+factorial); } } }