If you want to receive data of any length and avoid garbled characters, you can useBufferedreaderClass
Public class bufferedreader extends Reader
Because the input data may contain Chinese characters, the complete stream is used here. Bufferedreader reads content from the buffer. All input bytes are stored in the buffer zone.
System. In itself represents inputstream (byte stream). Currently, it is required to receive a response stream. You need to convert the byte stream into a response stream.Inputstreamreader
1 Import Java. Io. bufferedreader;
2 Import Java. Io. ioexception;
3 Import Java. Io. inputstreamreader;
4
5 Public Class Test33 {
6 Public Static Void Main (string [] ARGs) Throws Ioexception {
7 Bufferedreader reader = New Bufferedreader ( New Inputstreamreader (system. In ));
8 String STR = reader. Readline ();
9 System. Out. println (STR );
10 }
11 }
After jdk1.5, Java provides a special input data class. This class can complete the bufferedreader class function, and can also easily verify the input data. This class is stored inJava. utilPackage in progress
Use NotePad to receive input data from the keyboard:
1 ImportJava. util. vendor;
2
3 Public ClassTest34 {
4Public Static VoidMain (string [] ARGs ){
5Scanner S =NewUsing (system. In );
6String STR = S. Next ();
7System. Out. println (STR );
8}
9}
It is more convenient than directly using bufferedreader, but thisProgramThere is a problem. If the input string contains spaces, it will end. If we want to receive spaces, the separator will be changed to "\ n ".
1 Import Java. util. vendor;
2
3 Public Class Test34 {
4 Public Static Void Main (string [] ARGs ){
5 Scanner S = New Using (system. In );
6 S. usedelimiter ("\ n "); // Use Separators
7 String STR = S. Next ();
8 System. Out. println (STR );
9 }
10 }