Java standard stream

In this chapter you will learn:

  1. How to use Java standard output stream
  2. How to use Java standard input stream

Java standard output stream

static PrintStream out wraps the "standard" output stream. We usually use the standard output stream to output information to the console.

public class Main {
// j  a  va 2s.  c  om
  public static void main(String args[]) {
    System.out.println("Java2s.c o m");

  }
}

The output:

Java standard input stream

static InputStream in wraps the "standard" input stream. We can use the standard input stream to read user input from console window.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/* j  av a  2 s . c om*/
public class Main {
  public static void main(String[] args) {
    try {
      // read value from console window
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Please enter a value");
      System.out.println(br.readLine());
    } catch (IOException ioe) {
      System.out.println("IO Error :" + ioe);
      System.exit(0);
    }
  }
}

The following code reads integer value from console window.

It reads the value as a string type first then convert it to integer with Integer.parseInt method.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*ja v  a2 s.  co  m*/
public class Main {
  public static void main(String[] args) {
    try {
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.println("Please enter an integer value");
      String input = br.readLine();

      System.out.println("input:" + input);
      System.out.println("convert to integer:" + Integer.parseInt(input));
    } catch (NumberFormatException ne) {
      System.out.println("Invalid value" + ne);
      System.exit(0);

    } catch (IOException ioe) {
      System.out.println("IO Error :" + ioe);
      System.exit(0);
    }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. Get/set system property
  2. Predefined System properties
  3. Get the system properties
  4. Set a system properties
  5. Get the system environment
Home » Java Tutorial » Utility Classes
Java standard stream
Java system property get and set
Current time in millis second and nano second
Random UUID
JVM memory
JVM garbage collector
JVM shutting down
Processor count
OS system commands
Random class
Random value
Random value range
Compile Java source code
Timer and TimerTask