Predefined Streams in Java

Java, being a versatile and widely-used programming language, provides a rich set of features to handle input and output operations efficiently. Predefined streams are an integral part of this functionality, allowing Java programs to interact with their environment, including reading data from external sources and writing output to various destinations.

In this section, we will delve into the world of predefined streams in Java, exploring what they are, how they work, and how we can leverage them in programs.

Streams in Java

In Java, a stream is a sequence of data elements that supports various operations to perform computations. Streams can be of two types: input and output streams.

  • Input Streams: These streams are used for reading data from a source. They provide methods for reading bytes or characters from different types of sources, such as files, network connections, or in-memory data.
  • Output Streams: On the other hand, output streams are used for writing data to a destination. They offer methods for writing bytes or characters to various targets, like files, network connections, or in-memory buffers.

Predefined Streams in Java

Java provides three predefined streams that are automatically available to every Java program:

System.in:

Type: InputStream

Description: The stream represents the standard input, which is typically connected to the keyboard. It allows you to read data entered by the user.

System.out:

Type: PrintStream

Description: The stream represents the standard output, which is usually connected to the console. It is used for printing output from your program.

System.err:

Type: PrintStream

Description: Similar to System.out, it is used for output, but it's typically reserved for error messages.

These predefined streams are static variables of the java.lang.System class and are created when the Java Virtual Machine (JVM) starts up. They are connected to the console by default, but they can be redirected to other sources or destinations, providing great flexibility in handling input and output.

Working with Predefined Streams

Reading Input with System.in

To read data from the user through the standard input stream, you can use classes like InputStreamReader and BufferedReader. Here is an example of reading a line of text from the user:

Filename: ReadingInput.java

Output:

Enter your name: Adam 
Hello, Adam!

Writing Output with System.out and System.err

Printing output is straightforward using the System.out and System.err streams. You can use the print() and println() methods for this purpose:

Filename: PrintMsg.java

Output:

This is standard output.
This is an error message.

Redirecting Predefined Streams

It's possible to redirect these predefined streams to different sources or destinations. This can be useful for automated testing or for interacting with files or other I/O resources.

Filename: GreetingProgram.java

Output:

Enter your name: Manoj
Hello, Manoj!

Conclusion

Predefined streams in Java are fundamental tools for handling input and output operations in your programs. Understanding how to effectively use System.in, System.out, and System.err can greatly enhance your ability to create interactive and functional applications. Additionally, being able to redirect these streams provides a powerful mechanism for automating tasks and interacting with various I/O sources.






Latest Courses