How can we accept input from user in Java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can we accept input from user in Java

hi guys! I always get confused while using ioStreams in java. This is quite lengthy n confusing than c n c++. I can not determine that which class should I refer with different data types. which one is better n easy to use under different situations? BufferedReader, Input/Output StreamReader, console, scanner, DataInputOutput. pl reply!

23rd Jan 2017, 4:41 PM
Rupal Gujrathi
Rupal Gujrathi - avatar
2 Answers
+ 1
InputStream -> To read data from a source. OutputStream -> To write data to a destination. ByteStreams -> Classes related to 8-bit Bytes. Most important among them include 1)File InputStream-For reading data from files. 2)File OutputStream-To create a file and/or write data into it. // Headers used include import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.FileInputStream; import java.io.FileOutputStream; //etc. //Sample program with Buffered Reader public class NewClass { public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("Enter content"); String s = br.readLine(); System.out.println("Entered content is "+s); } }
24th Jan 2017, 6:41 AM
Jidesh
+ 1
ty jidesh
26th Jan 2017, 6:57 AM
Rupal Gujrathi
Rupal Gujrathi - avatar