Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java System Stream

In Java, the System class provides three predefined streams to interact with standard input, output, and error devices, usually the keyboard and console. These streams are static members of the System class and include:

  • System.in: Standard input stream, typically connected to the keyboard.
  • System.out: Standard output stream, typically connected to the console.
  • System.err: Standard error stream, typically connected to the console.

Let's explore how to use these streams in your Java programs.

  • Reading from the Standard Input Stream (System.in):

System.in is an instance of the InputStream class. To read data from the standard input stream, you can use a wrapper class like Scanner or BufferedReader.

Example using Scanner:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter your name: ");
        String name = scanner.nextLine();
        System.out.println("Hello, " + name + "!");
    }
}

Example using BufferedReader:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter your name: ");
        try {
            String name = reader.readLine();
            System.out.println("Hello, " + name + "!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • Writing to the Standard Output Stream (System.out):

System.out is an instance of the PrintStream class. You can use various print, println, and printf methods to write data to the standard output stream.

Example:

public class Main {
    public static void main(String[] args) {
        System.out.print("Hello, World!"); // No newline
        System.out.println("Hello, World!"); // With newline
        System.out.printf("Hello, %s!", "World"); // Formatted output
    }
}
  • Writing to the Standard Error Stream (System.err):

System.err is also an instance of the PrintStream class. It is commonly used to display error messages or diagnostic information. It behaves similarly to System.out and provides the same print, println, and printf methods.

Example:

public class Main {
    public static void main(String[] args) {
        System.err.print("Error: Something went wrong!"); // No newline
        System.err.println("Error: Something went wrong!"); // With newline
        System.err.printf("Error: %s%n", "Something went wrong!"); // Formatted output
    }
}

In conclusion, the System class in Java provides predefined streams System.in, System.out, and System.err for standard input, output, and error devices. You can use these streams to read from the keyboard, write to the console, and display error messages or diagnostic information.

  1. Redirecting output to System.out in Java: Redirecting output involves changing the default output stream using System.setOut().

    PrintStream originalOut = System.out; // Save original output stream
    System.setOut(new PrintStream(new FileOutputStream("output.txt")));
    System.out.println("This goes to the file.");
    System.setOut(originalOut); // Restore original output stream
    
  2. Java System.in input stream usage: System.in is the standard input stream and can be used to read data from the console.

    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter your name: ");
    String name = scanner.nextLine();
    System.out.println("Hello, " + name + "!");
    
  3. Handling errors with System.err stream in Java: System.err is the standard error stream used for error messages.

    System.err.println("Error: Something went wrong!");
    
  4. Piping and redirecting streams in Java: Streams can be piped or redirected to connect the output of one stream to the input of another.

    InputStream inputStream = new FileInputStream("input.txt");
    OutputStream outputStream = new FileOutputStream("output.txt");
    
    byte[] buffer = new byte[1024];
    int bytesRead;
    
    while ((bytesRead = inputStream.read(buffer)) != -1) {
        outputStream.write(buffer, 0, bytesRead);
    }
    
    inputStream.close();
    outputStream.close();
    
  5. Flushing streams in Java with System.flush(): System.flush() is not available. Flushing is typically performed directly on the stream objects.

    System.out.flush(); // This will not work
    
  6. Using PrintStream with System.out in Java: PrintStream is often used with System.out for formatted output.

    PrintStream printStream = new PrintStream(System.out);
    printStream.println("This is printed using PrintStream.");
    
  7. Java System.console() for interactive input: System.console() returns the unique console object associated with the current Java virtual machine.

    Console console = System.console();
    
    if (console != null) {
        String input = console.readLine("Enter something: ");
        console.writer().println("You entered: " + input);
    }
    
  8. Java System.setOut() and System.setErr() methods: System.setOut() and System.setErr() are used to redirect the standard output and error streams.

    System.setOut(new PrintStream(new FileOutputStream("output.txt")));
    System.setErr(new PrintStream(new FileOutputStream("error.txt")));
    
  9. Java System.setIn() method for redirecting input: System.setIn() is used to redirect the standard input stream.

    System.setIn(new FileInputStream("input.txt"));
    Scanner scanner = new Scanner(System.in);
    
  10. Java System.setSecurityManager() and custom streams: System.setSecurityManager() sets the system-wide security manager.

    System.setSecurityManager(new SecurityManager());
    
  11. Java System streams vs PrintWriter and Scanner: PrintWriter and Scanner provide additional features compared to System.out and System.in, such as formatted output and advanced input parsing.

    PrintWriter writer = new PrintWriter(System.out);
    Scanner scanner = new Scanner(System.in);
    
  12. Java System.out.print() vs System.out.println(): System.out.print() prints without a newline, while System.out.println() adds a newline.

    System.out.print("Hello");
    System.out.println(" World!");
    
  13. Java System.in.read() and reading from the console: System.in.read() reads a single byte from the console.

    int byteRead = System.in.read();