Java-note-input stream

Source: Internet
Author: User

Unlike the powerful functions such as scanf in C, java mostly defines input and output stream objects.
Common classes include BufferedReader and consumer.
Instance program:
1. Use flock to read integer or float data from the keyboard

Java code
      java.util.*                               Scanner in= Scanner(System.in);              System.out.println("please input a float number"              a=in.nextFloat();               System.out.println("please input a integer number"              b=in.nextInt();        }  

 

Ii. Use bufferedreaderto read the strings from the keyboard and write them into the file abc.txt.

Java code
     java.io.*    main(String[] args) =  BufferedReader (=  BufferedWriter( FileWriter("abc.txt"=(!str.equals("exit"=

 

 

About the JDK1.5 sequence class

Struct is a new class of SDK1.5, but an object is created using this class.
Internal reader = new external (System. in );

The reader object then calls the following methods (functions) to read various data types that the user inputs on the command line:
Next. Byte (), nextDouble (), nextFloat, nextInt (),

NextLine (), nextLong (), nextShot ()



 

Iii. Differences between consumer and BufferedReader


To input data to a program in command line mode, we can use the standard input string object System. in. however, we do not often use it directly because System. the read method provided by in can only read one byte of data at a time. What we usually use is to read a string or a number. Therefore, the read method provides the following functions, it is not very useful for us.
In Java SE 6, you can use the inputs class to obtain user input. The inputs class is located in java. in the util package, if you want to use the token to obtain user input, add import java. util. statement. the import function tells the compiler that you will use java. the category class in the util package.
Let's look at an example:

Java code
Scanned scan = System. out. println ("enter a string:" System. out. println ("your input string is:" +}

 

 

Run the above program and you will see that the string you entered will be displayed as is below.
Let's take a look at the meaning of each statement in this program:
New is to create an object. new in the program means to create a scan object of the category class. however, you must use System. as its parameter, in can also be considered as System. supporter of the in object, System. after obtaining the user input content, the in operator is handed over to the operator for some processing.
The Methods class provides multiple methods:
Next (): gets a string;
NextInt (): converts the obtained string to an integer of the int type;
NextFloat (): converts the obtained string to the float type;
NextBoolean (): converts the obtained string to the boolean type;

It is very convenient to use the token to obtain user input ,. when the content you enter contains spaces, it is clear that the string you entered cannot be completely obtained using the delimiter. at this time, we can consider using the BufferedReader class to obtain the input. in fact, in Java SE 1.4 and earlier versions, the callback method has not yet been provided. BufferReader is also used when we get the input.
The BufferedReader class is located in the java. io package. To use this class, we need to introduce the java. io package: import java. io. BufferedReader.
The readLine () method of the BufferedReader object must handle java. io. IOException (Exception ).
It is much more complicated to use BufferedReader to obtain input. However, this method is fixed and can be processed before each use.

BufferedReader buffer = new BufferedReader (new InputStreamReader (System. in ));
String text = buffer. readLine ();
The readLine () method returns all character input before the user presses the Enter key, excluding the return character of the last press Enter.
The complete example program is as follows:

Java code
Main (String [] args) BufferedReader buffer = BufferedReader (System. out. println ("enter a String" String text = System. out. println ("the string you entered is:" +}

 

4. Use System. in. read to read char data from the keyboard

Java code
 System.out.println("please input a char"          c=(         System.out.println(c);  

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.