A stream is a water body that carries water as it flows from one point(source) to another(destination). In programming terminology, a stream can be a continuous group of data or a channel through which data travels from one point(source) to another(destination). An input stream receives data from a source into a program and an output stream sends data to a destination from the program.
Java Stream Example
class BasicIO
{
public static void main(String aa[])
{
byte by[]=new byte[255];
try
{
System.out.println(“Enter a line of text”);
System.in.read(by,0,255);
System.out.println(“The line typed was”);
String stri=new String(by,”Default”);
System.out.println(stri);
}
catch(Exception excep)
{
System.out.println(“Error occurred”);
}
}
}
Explanation.
The standard input/output stream in java is represented by 3 members of the system class: in, out, and err. These represent the major byte streams provided by java.
System. in—The standard input stream is used for reading characters of data.
System. out—The standard output stream is used to typically display the output on the screen.
System. err—This is the standard error stream.
In this example…
The objective of the code listed in this example is to accept a line from the keyboard and print it on the screen.
A byte array that can hold 255 bytes is created at the beginning of the program. The code System.in.read(by,0,255); allows us to read 255 bytes of data from the keyboard into the byte array.
Then we create a String instance based on the byte array and print it on the screen using the following code.
String str=new String(by,”Default”);
System.out.println(str);
It is likely that the operation of reading and writing may cause an exception therefore we enclose the code that performs read and write operations within a try-catch block.