Java console input and output detailed

Source: Internet
Author: User

Beginner Java, perhaps we have encountered a problem, from the console to get the characters, the most common is through System.in.read (); Get the characters you entered, the code is as follows:

public static void Receiveonechar () {//Gets an input character
  char ch= ' 2 ';
     System.out.println ("Please enter a number:");
     try {
   ch= (char) System.in.read ();
  } catch (IOException e) {
   e.printstacktrace ();
  }
     SYSTEM.OUT.PRINTLN ("Your entered char is:" +ch);
  }

But this method can only get an input character, in order to get more input characters, or to achieve a,b addition how do we achieve it? Look below:

We know that Java is input into the stream, where the system class manages the basic input output stream and the error stream.

System.out the stream to the default location

System.in Get input

System.err Output error message

Each of these three objects is automatically generated whenever the main method is executed;

The basic input stream InputStream is an abstract class that has the following methods for inheriting subclasses:

Read (); Reading data from the stream;

Skip (); Skip several bytes in the stream;

Available (); Returns the number of bytes available in the stream;

Mark (); marks a position in the stream;

Reset (); Returns the position of the marker;

Marksupport (); Whether to support marking and reset operation;

Close ();

Among them, the read () method has three versions;

int read (); read an integer;

int read (byte[] b); Read multiple bytes to an array of bytes;

int read (byte[] b,int off,int len); Read len-length bytes from one location of the byte array;

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.