Usage of system. In. Read ()

Source: Internet
Author: User
Document directory
  • Read
  • Read
  • Read

 

Usage of system. In. Read ()

22:00:24 | category:

Default category | Tag:
| Large font size, medium/small subscription

It is required to read the keyboard input.
1. Input stream system. In;
2. Character input stream inputstreamreader
3. Cache input stream bufferedreader
4. Cache input stream method Readline () // Chinese meaning: Read a row (with the Enter key)
Code: bufferedreader reader = new bufferedreader (New inputstreamreader (system. In ));

System. In. Read () returns an integer byte of data, which indicates the byte. Therefore, it is the first byte of Unicode or the ASCII value of the character. This method reads data from a stream one by one, so it is an iterative process. We can see that in is a static stream, so there is only one stream in this program. Calling System. In. Read () repeatedly is actually traversing every byte of data in the stream. The most common stream is the keyboard input stream. You can enter a string on the keyboard (press the Enter key to represent the two characters \ r \ n, and the ASCII value of \ r is 10 and \ n is 13 ). We can call system. In. Read () repeatedly to read the bytes (ASCII value) represented by characters in the string input from the keyboard ).
public final class System extends Object
 

SystemThe class contains some useful class fields and methods. It cannot be instantiated.

InSystemThe facilities provided by the class include standard input, standard output, and error output streams, access to externally defined attributes and environment variables, and methods for loading files and libraries; there is also a practical method to quickly copy part of the array.

Public static final inputstream in

"Standard" input stream. This stream has been opened and is ready to provide input data. Generally, this stream corresponds to a keyboard input or another input source specified by the host environment or user.

Read
public abstract int read()                    throws IOException
The next byte that reads data from the input stream. Return 0To 255Within the scope intByte value. If no byte is available because it has reached the end of the stream, the returned value is
-1. This method is blocked until the input data is available, the end of the stream is detected, or an exception is thrown.

Subclass must provide an implementation of this method.

Return Value:
Next Data byte; if it reaches the end of the stream, return -1.
Throw:
IOException-If an I/O error occurs.

Read
public int read(byte[] b)           throws IOException
Reads a certain number of bytes from the input stream and stores them in the buffer array. b. Returns the actual number of bytes read as an integer. This method is always blocked until the input data is available, the end of the file is detected, or an exception is thrown.

IfbIf the length is 0, no Bytes are read and0Otherwise, try to read at least one byte. If the stream is at the end of the file and there are no available bytes, the returned value is
-1Otherwise, read at least one byte and store it inb.

Store the first byte read in the elementb[0]Inb[1]And so on. The maximum number of bytes read is equal
b
. SetKThe number of bytes actually read. These bytes are stored inb[0]Tob[K-1]Does not affect
b[K]Tob[b.length-1].

ClassInputStreamOfread(b)The effect of the method is equivalent:

 read(b, 0, b.length) 

Parameters:
b-Buffer for storing read data.
Return Value:
The total number of bytes read into the buffer. If no data is available because it has reached the end of the stream -1.
Throw:
IOException-If the stream is not at the end of the file, the first byte cannot be read. If the input stream is closed, other I/O errors may occur.
NullPointerException-If
bIs null.
For more information, see:
read(byte[], int, int)

Read
public int read(byte[] b,                  int off,                  int len)           throws IOException
Maximum number of input streams lenBytes are read into the byte array. Try to read lenBut the read bytes may be smaller than this value. Returns the actual number of bytes read as an integer.

This method is blocked until the input data is available, the end of the stream is detected, or an exception is thrown.

IflenIf the value is 0, no Bytes are read and the return value is0Otherwise, try to read at least one byte. If the stream is at the end of the file and there are no available bytes, the returned value is
-1Otherwise, read at least one byte and store it inb.

Store the first byte read in the elementb[off]Inb[off+1]And so on. The maximum number of bytes read is equal
len. SetKThe number of bytes actually read. These bytes are stored inb[off]Tob[off+K-1]Does not affect
b[off+K]Tob[off+len-1].

In any situation,b[0]Tob[off]Andb[off+len]To
b[b.length-1]Will not be affected.

ClassInputStreamOfread(b, off, len)Method repeated call Method
read(). If the first such call causesIOExceptionFromread(b,
off, len)This exception is returned when a method is called. Ifread()Any subsequent call
IOExceptionTo capture the exception and consider it as reaching the end of the file; the bytes read at this point are stored inbAnd returns the number of bytes read before an exception occurs. After reading the input data
lenThe default implementation of this method will be blocked until the number of requests, the end mark of the file is detected, and an exception is thrown. It is recommended that subclass provide more effective implementation of this method.

Parameters:
b-Buffer for reading data.
off-Array bThe initial offset of the data to be written.
len-Maximum number of bytes to read.
Return Value:
The total number of bytes read into the buffer. If no data is available because it has reached the end of the stream -1.
Throw:
IOException-The first byte cannot be read because it is not at the end of the file; if the input stream is closed; if other I/O errors occur.
NullPointerException-If
bIs null.
IndexOutOfBoundsException-If
offNegative, lenIs negative, or lenGreater b.length - off
 

Import java. Io .*;

Public class Io {

Public static void main (string ARGs []) throws ioexception {

System. Out. println ("Calculate rectangular area ");

System. Out. println ("Enter the length :");

Int A, B;

A = system. In. Read ();

System. Out. println ("Enter the width ");

B = system. In. Read ();

System. Out. println ("calculates the rectangle area:" + A * B );

}

}

Result:

Calculate the rectangular area

Enter the length:

1

Enter the width

The area of the calculated rectangle is: 637

The problem is, when I input the width, I will output 637 directly ??

Why? What does 637 mean ??

Explain system. I. Read ()

 

Best Answer

System. In. Read receives bytes 0-255.

After you enter a value of 1, the returned ASCII code is 49.

Then you press enter again. The ASCII code of the carriage return is 13.

The result is equivalent

A = 49

B = 13

System. In. Read () can be used to input characters and return Unicode codes. However, the disadvantage is that only one character can be entered, as shown in figure
Public class Exec
{
Public static void main (string ARGs []) throws exception
{
Int I = system. In. Read ();
System. Out. println (I );
}
}
If you enter 1
Output is 49
If you enter 123
Output or 49

The system. In. Read () overload function can be used to input multiple characters, as shown in
Public class Exec
{
Public static void main (string ARGs []) throws exception
{
Byte [] barray = new byte [5];

System. In. Read (barray );

For (INT I = 0; I
System. Out. println (barray [I]);
}
}
If you enter 1
The output is
49
13
10
0
0
If you enter 12
The output is
49
50
13
10
0

At this time, you can find that the entered Unicode code is still in use, but many other characters are generated, such as carriage return and line feed.
To further convert byte array information to the required type, we need to combine the stream class for processing.

To enter characters or numbers, use the input stream, such as datainputstream, to assist in conversion, for example:

Import java. Io .*;
Public class
{
Public static void main (string ARGs [])
{
Double X = 0, y = 0;
Datainputstream in = new datainputstream (system. In );
System. Out. println ("input x value :");
Try
{
X = double. parsedouble (in. Readline ());
System. Out. println ("x value:" + x );
} Catch (exception E)
{
System. Out. println ("wrong !!!!!!!! ");
}
System. Out. println ("input value of Y :");
Try
{
Y = double. parsedouble (in. Readline ());
System. Out. println ("Y value:" + y );
} Catch (exception E)
{
System. Out. println ("wrong !!!!!!!! ");
}

System. Out. println (math. Pow (x, y ));
}
}

Or:

Import java. Io .*;
Public class sum {
Public static void main (string ARGs []) {
Try {
Bufferedreader reader = new bufferedreader (New inputstreamreader (system. In ));
Int A = integer. parseint (reader. Readline ());
System. Out. println ();
}
Catch (exception e ){
System. Out. println (E );
}
}
}

Bufferedreader reader = new bufferedreader (New inputstreamreader (system. In ));
Inputstreamreader is a specific class used to read binary input streams like reading the primary stream. put in the inputstreamreader object, and then buffer it by using the bufferedreader object to improve the efficiency of input operations.

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.