System in type java lang

Java System.in, System.out, and System.error

Java has 3 streams called System.in, System.out, and System.err which are commonly used to provide input to, and output from Java applications. Most commonly used is probably System.out for writing output to the console from console programs (command line applications).

System.in, System.out and System.err are initialized by the Java runtime when a Java VM starts up, so you don’t have to instantiate any streams yourself (although you can exchange them at runtime). I will explain each of these streams in deeper detail later in this tutorial.

System.in

System.in is an InputStream which is typically connected to keyboard input of console programs. In other words, if you start a Java application from the command line, and you type something on the keyboard while the CLI console (or terminal) has focus, the keyboard input can typically be read via System.in from inside that Java application. However, it is only keyboard input directed to that Java application (the console / terminnal that started the application) which can be read via System.in. Keyboard input for other applications cannot be read via System.in .

System.in is not used as often since data is commonly passed to a command line Java application via command line arguments, files, or possibly via network connections if the application is designed for that. In applications with GUI the input to the application is given via the GUI. This is a separate input mechanism from System.in.

Читайте также:  Модуль php для htaccess

System.out

System.out is a PrintStream to which you can write characters. System.out normally outputs the data you write to it to the CLI console / terminal. System.out is often used from console-only programs like command line tools as a way to display the result of their execution to the user. This is also often used to print debug statements of from a program (though it may arguably not be the best way to get debug info out of a program).

System.err

System.err is a PrintStream . System.err works like System.out except it is normally only used to output error texts. Some programs (like Eclipse) will show the output to System.err in red text, to make it more obvious that it is error text.

Simple System.out + System.err Example:

Here is a simple example that uses System.out and System.err :

Exchanging System Streams

Even if the 3 System streams are static members of the java.lang.System class, and are pre-instantiated at JVM startup, you can change what streams to use for each of them. Just set a new InputStream for System.in or a new OutputStream for System.out or System.err , and all further data will be read / written to the new stream.

To set a new System stream, use one of th emethods System.setIn() , System.setOut() or System.setErr() . Here is a simple example:

OutputStream output = new FileOutputStream("c:\\data\\system.out.txt"); PrintStream printOut = new PrintStream(output); System.setOut(printOut);

Now all data written to System.out should be redirected into the file «c:\\data\\system.out.txt». Keep in mind though, that you should make sure to flush System.out and close the file before the JVM shuts down, to be sure that all data written to System.out is actually flushed to the file.

Читайте также:  Rotate an image in html

Источник

Java: System.out Vs System.in Vs System.err classes and methods.

The java.lang.System class, having three static members (in, out and err) that pre-initialize at JVM startup that’s what no need to initialize. These classes also known as Stream classes.

Out of these System.out is most frequent use by programmers to write output on the console.

System Stream Classes

Here is a list of System Stream classes objects:

System.in

System.in is an InputStream type object which is typically connected to the keyboard input of console programs.

Note: It’s generally used often, Mainly used by beginner-level programmers to insert input from the keyboard. Because when you consider for application-level data is commonly passed to program by:

System.out

System.out is a PrintStream type used to outputs the data to the console.

Note: It’s the most frequent used Stream object to print out of the program and for debugging about the flow and values.

System.err

System.err is a PrintStream type mainly used to output an error texts to console.

Note: Some of IDE like eclipse show System.err output in the console in red color so that you can easily see error messages.

Example for System .in, System.out and System.err

In this example, you will see, How to insert value to a program by the keyboard by System.in. Output to console by System.out and for any error print by System.err.

System in out and Err Example

Output

System in out err result

Exchanging System Streams

By default System Stream class is to console, but we can also change System Streams by programmatical configuration for each type so that InputStream for System.in or OutputStream for System.out or System.err will read and write to the new stream.

  • System.setIn() : This method use to change input stream.
  • System.setOut() : This method use to change output stream.
  • System.setErr() : This method use to change error output stream.
Читайте также:  Зачем нужен construct php

System.out Stream Change

In the below example, out will be got to the given file instead of default OutputStream as Console.

OutputStream output = new FileOutputStream("c:\\output\\FacingIssuesOnIT_logs.txt"); PrintStream printOut = new PrintStream(output); //This method will change output stream. System.setOut(printOut);

Share this with others:

Источник

Оцените статью