Predefined Streams in JavaJava, 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 JavaIn 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.
Predefined Streams in JavaJava 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 StreamsReading Input with System.inTo 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.errPrinting 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 StreamsIt'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! ConclusionPredefined 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. Next TopicPrime Factor Program in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India