Java Predefined Streams
Java is one of the languages that includes some predefined streams for its users. The java.lang.System class provides three Java Predefined streams, sometimes known as standard streams.
Java Standard Streams
As far as we know, the java. All Java programs automatically import lang package. The System class, defined in this package, contains multiple run-time environment features. For example, we may get specific system parameters' current time and configuration by utilizing some of its functions.
The three predefined stream variables, in, out, and err, are likewise included in the system class. Within the system, these fields are designated as static and public. This implies they don't require reference to a particular System object and can be utilized by any other section of our programs.
System.out is the standard output stream. This is the console by default. Standard input, the keyboard by default, is called system.in. The standard error stream, which is also the console by default, is referred to as system.err.
These streams can, however, be diverted to any appropriate input/output device. System.out and System.err are objects of type PrintStream, while System.in is an object of type InputStream. Even though they are usually used to read and write characters to and from the terminal, they are byte streams.
If we would like, we may include these in character-based streams. System.out has been used in the examples in the previous chapters. We can use System.err in several ways.
- System.in
It is the input standard stream. By default, this stream is used to read data from the keyboard for the program.
- System.out
It is the output standard stream. This stream is used to write data from the program to a specified file or, by default, an output device like a monitor or console.
- System.err
It is an error standard stream. This default displays an error message to users on the screen or console.
One of InputStream's objects is System.in. However, System.out and System.err are both PrintStream-type objects.
The Java Virtual Machine (JVM) automatically initializes these Java Predefined Streams at the start. Although they are all byte streams, these can also read and write character streams.
Let us have a close look at these streams one at a time.
Java Predefined Stream for Standard Input
Java uses a stream called System.in for standard input. This stream belongs to the InputStream stream object. In Java, obtaining a character-based stream requires enclosing System.in in a BufferedReader object.
The following code is used to accomplish this:
BufferedReader br = new BufferedReader (new InputStreamReader (System.in) );
In this case, br is the character-based stream connected via System.in, and InputStreamReader is the input stream you are establishing. The br object can now be used to read user input.
The following program to demonstrate how to use Java Predefined Streams' System.in
File name: Input.java
import java.io.*;
public class Input
{
public static void main( String args[] ) throws IOException
{
String name;
BufferedReader br = new BufferedReader ( new InputStreamReader (System.in) );
System.out.println ( "what is your name?" );
name = br.readLine();
System.out.println ("Your name is:" + name);
}
}
Output:
what is your name?
Kathy
Your name is: Kathy
Because of its complexity, this stream is not utilized very often because, depending on the program, inputs are either supplied via a GUI or the command line. System.out is often used to accomplish the functions of System.in in terminal apps and applets.
Java Predefined Stream for Standard Output
A component of the PrintStream stream is System.out. In Java, standard output is sent to this stream. The software defaults to sending this stream's output to the console. Take a look at the example below.
The code's System.out.println("Your city is:" + city); statement controls where the program output goes on the user's console.
Using the following Java program, let us understand Java Predefined Stream for Standard Output.
File name: Input1.java
import java.io.*;
public class Input1
{
public static void main( String args[] ) throws IOException
{
String city;
BufferedReader br = new BufferedReader ( new InputStreamReader (System.in) );
System.out.println( "what is your city?" );
city = br.readLine();
System.out.println("Your city is: " + city);
}
}
Output:
what is your city?
Hyderabad
Your city is: Hyderabad
Java Predefined Stream for Standard Error
Java uses the stream System.err for standard error. The stream is a PrintStream stream object.
System.err is usually used inside the try/catch block's catch section. Here is an example of the block:
try
{
// Code for execution
}
catch ( Exception e) {
System.err.println ( “Error in code: ” + e );
Let us try to understand Java Predefined Stream for Standard Error using the following java program.
File name: Error.java
public class Error {
public static void main(String[] args) {
try {
// Some code that may throw an exception
int result = 10/0;
System.out.println("Result: " + result);
} catch (ArithmeticException e) {
// Log the error message to the standard error stream
System.err.println("Error: Division by zero is not allowed");
}
}
}
Output:
Error: Division by zero is not allowed