InputStream Reading Methods:
Using System.in for Reading:
Reading Console Input: System.in is used as input stream, where pressing "ENTER" indicates the end of the input stream.
ReadBytes Code Example:
Functionality: The program reads a byte array from the console and displays the entered characters.
Code Structure:
data[]: 10-byte array to store the input.
System.in.read(data): Reads the characters typed into the console and stores them in data.
Display Loop: Iterates over data[] to convert each byte to character and display them.
Example Code:
import java.io.*; class ReadBytes { public static void main(String args[]) throws IOException { byte data[] = new byte[10]; System.out.println("Enter some characters."); System.in.read(data); // Lê o array de bytes System.out.print("You entered: "); for (int i = 0; i < data.length; i++) System.out.print((char) data[i]); // Converte e exibe cada byte como caractere } }
Execution Example:
Input: "Read Bytes"
Output:
Enter some characters. You entered: Read Bytes
This excerpt details how to use the read() methods to read keyboard data in byte format, illustrating the basic use of System.in for console input and demonstrating direct manipulation of bytes in an array.
The above is the detailed content of Reading console input. For more information, please follow other related articles on the PHP Chinese website!