Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels) Summary: [Methods](https://developer.android.com/reference/java/io/Console#pubmethods) \| [Inherited Methods](https://developer.android.com/reference/java/io/Console#inhmethods)

# Console


`
public

final

class
Console
`


`

extends https://developer.android.com/reference/java/lang/Object


`

`


implements

https://developer.android.com/reference/java/io/Flushable


`

|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object) ||
| ↳ | java.io.Console |

<br />

*** ** * ** ***

Methods to access the character-based console device, if any, associated
with the current Java virtual machine.

Whether a virtual machine has a console is dependent upon the
underlying platform and also upon the manner in which the virtual
machine is invoked. If the virtual machine is started from an
interactive command line without redirecting the standard input and
output streams then its console will exist and will typically be
connected to the keyboard and display from which the virtual machine
was launched. If the virtual machine is started automatically, for
example by a background job scheduler, then it will typically not
have a console.


If this virtual machine has a console then it is represented by a
unique instance of this class which can be obtained by invoking the
`https://developer.android.com/reference/java/lang/System#console()` method. If no console device is
available then an invocation of that method will return `null`.


Read and write operations are synchronized to guarantee the atomic
completion of critical operations; therefore invoking methods
`https://developer.android.com/reference/java/io/Console#readLine()`, `https://developer.android.com/reference/java/io/Console#readPassword()`, `https://developer.android.com/reference/java/io/Console#format(java.lang.String,%20java.lang.Object[])`,
`https://developer.android.com/reference/java/io/Console#printf(java.lang.String,%20java.lang.Object[])` as well as the read, format and write operations
on the objects returned by `https://developer.android.com/reference/java/io/Console#reader()` and `https://developer.android.com/reference/java/io/Console#writer()` may
block in multithreaded scenarios.


Invoking `close()` on the objects returned by the `https://developer.android.com/reference/java/io/Console#reader()`
and the `https://developer.android.com/reference/java/io/Console#writer()` will not close the underlying stream of those
objects.


The console-read methods return `null` when the end of the
console input stream is reached, for example by typing control-D on
Unix or control-Z on Windows. Subsequent read operations will succeed
if additional characters are later entered on the console's input
device.


Unless otherwise specified, passing a `null` argument to any method
in this class will cause a `https://developer.android.com/reference/java/lang/NullPointerException` to be thrown.


**Security note:**
If an application needs to read a password or other secure data, it should
use `https://developer.android.com/reference/java/io/Console#readPassword()` or `https://developer.android.com/reference/java/io/Console#readPassword(java.lang.String,%20java.lang.Object[])` and
manually zero the returned character array after processing to minimize the
lifetime of sensitive data in memory.

>     Console cons;
>     char[] passwd;
>     if ((cons = System.console()) != null &&
>         (passwd = cons.readPassword("[%s]", "Password:")) != null) {
>         ...
>         java.util.Arrays.fill(passwd, ' ');
>     }

<br />

## Summary

| ### Public methods ||
|---|---|
| ` https://developer.android.com/reference/java/nio/charset/Charset` | ` https://developer.android.com/reference/java/io/Console#charset()() ` Returns the `https://developer.android.com/reference/java/nio/charset/Charset` object used for the `Console`. |
| ` void` | ` https://developer.android.com/reference/java/io/Console#flush()() ` Flushes the console and forces any buffered output to be written immediately . |
| ` https://developer.android.com/reference/java/io/Console` | ` https://developer.android.com/reference/java/io/Console#format(java.lang.String,%20java.lang.Object[])(https://developer.android.com/reference/java/lang/String fmt, https://developer.android.com/reference/java/lang/Object args) ` Writes a formatted string to this console's output stream using the specified format string and arguments. |
| ` https://developer.android.com/reference/java/io/Console` | ` https://developer.android.com/reference/java/io/Console#printf(java.lang.String,%20java.lang.Object[])(https://developer.android.com/reference/java/lang/String format, https://developer.android.com/reference/java/lang/Object args) ` A convenience method to write a formatted string to this console's output stream using the specified format string and arguments. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/io/Console#readLine(java.lang.String,%20java.lang.Object[])(https://developer.android.com/reference/java/lang/String fmt, https://developer.android.com/reference/java/lang/Object args) ` Provides a formatted prompt, then reads a single line of text from the console. |
| ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/io/Console#readLine()() ` Reads a single line of text from the console. |
| ` char[]` | ` https://developer.android.com/reference/java/io/Console#readPassword()() ` Reads a password or passphrase from the console with echoing disabled |
| ` char[]` | ` https://developer.android.com/reference/java/io/Console#readPassword(java.lang.String,%20java.lang.Object[])(https://developer.android.com/reference/java/lang/String fmt, https://developer.android.com/reference/java/lang/Object args) ` Provides a formatted prompt, then reads a password or passphrase from the console with echoing disabled. |
| ` https://developer.android.com/reference/java/io/Reader` | ` https://developer.android.com/reference/java/io/Console#reader()() ` Retrieves the unique `https://developer.android.com/reference/java/io/Reader` object associated with this console. |
| ` https://developer.android.com/reference/java/io/PrintWriter` | ` https://developer.android.com/reference/java/io/Console#writer()() ` Retrieves the unique `https://developer.android.com/reference/java/io/PrintWriter` object associated with this console. |

| ### Inherited methods |
|---|---|
| From class ` https://developer.android.com/reference/java/lang/Object ` |---|---| | ` https://developer.android.com/reference/java/lang/Object` | ` https://developer.android.com/reference/java/lang/Object#clone()() ` Creates and returns a copy of this object. | | ` boolean` | ` https://developer.android.com/reference/java/lang/Object#equals(java.lang.Object)(https://developer.android.com/reference/java/lang/Object obj) ` Indicates whether some other object is "equal to" this one. | | ` void` | ` https://developer.android.com/reference/java/lang/Object#finalize()() ` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. | | ` final https://developer.android.com/reference/java/lang/Class<?>` | ` https://developer.android.com/reference/java/lang/Object#getClass()() ` Returns the runtime class of this `Object`. | | ` int` | ` https://developer.android.com/reference/java/lang/Object#hashCode()() ` Returns a hash code value for the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notify()() ` Wakes up a single thread that is waiting on this object's monitor. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#notifyAll()() ` Wakes up all threads that are waiting on this object's monitor. | | ` https://developer.android.com/reference/java/lang/String` | ` https://developer.android.com/reference/java/lang/Object#toString()() ` Returns a string representation of the object. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long,%20int)(long timeoutMillis, int nanos) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait(long)(long timeoutMillis) ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*, or until a certain amount of real time has elapsed. | | ` final void` | ` https://developer.android.com/reference/java/lang/Object#wait()() ` Causes the current thread to wait until it is awakened, typically by being *notified* or *interrupted*. | ||
| From interface ` https://developer.android.com/reference/java/io/Flushable ` |---|---| | ` abstract void` | ` https://developer.android.com/reference/java/io/Flushable#flush()() ` Flushes this stream by writing any buffered output to the underlying stream. | ||

## Public methods

### charset

Added in [API level 35](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Charset charset ()
```

Returns the `https://developer.android.com/reference/java/nio/charset/Charset` object used for
the `Console`.


The returned charset corresponds to the input and output source
(e.g., keyboard and/or display) specified by the host environment or user.
It may not necessarily be the same as the default charset returned from
`https://developer.android.com/reference/java/nio/charset/Charset#defaultCharset()`.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/nio/charset/Charset` | a `https://developer.android.com/reference/java/nio/charset/Charset` object used for the `Console` <br /> |

### flush

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public void flush ()
```

Flushes the console and forces any buffered output to be written
immediately .

<br />

### format

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Console format (String fmt, 
                Object... args)
```

Writes a formatted string to this console's output stream using
the specified format string and arguments.

<br />

| Parameters ||
|---|---|
| `fmt` | `String`: A format string as described in [Format string syntax](https://developer.android.com/reference/java/util/Formatter.html#syntax) <br /> |
| `args` | `Object`: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java Virtual Machine Specification. The behaviour on a `null` argument depends on the [conversion](https://developer.android.com/reference/java/util/Formatter.html#syntax). <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Console` | This console <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/util/IllegalFormatException` | If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the [Details](https://developer.android.com/reference/java/util/Formatter.html#detail) section of the formatter class specification. |

### printf

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Console printf (String format, 
                Object... args)
```

A convenience method to write a formatted string to this console's
output stream using the specified format string and arguments.

An invocation of this method of the form
`con.printf(format, args)` behaves in exactly the same way
as the invocation of

```
con.format(format, args)
```
.

<br />

<br />

| Parameters ||
|---|---|
| `format` | `String`: A format string as described in [Format string syntax](https://developer.android.com/reference/java/util/Formatter.html#syntax). <br /> |
| `args` | `Object`: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The number of arguments is variable and may be zero. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java Virtual Machine Specification. The behaviour on a `null` argument depends on the [conversion](https://developer.android.com/reference/java/util/Formatter.html#syntax). <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Console` | This console <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/util/IllegalFormatException` | If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the [Details](https://developer.android.com/reference/java/util/Formatter.html#detail) section of the formatter class specification. |

### readLine

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String readLine (String fmt, 
                Object... args)
```

Provides a formatted prompt, then reads a single line of text from the
console.

<br />

| Parameters ||
|---|---|
| `fmt` | `String`: A format string as described in [Format string syntax](https://developer.android.com/reference/java/util/Formatter.html#syntax). <br /> |
| `args` | `Object`: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java Virtual Machine Specification. <br /> |

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | A string containing the line read from the console, not including any line-termination characters, or `null` if an end of stream has been reached. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOError` | If an I/O error occurs. |
| `https://developer.android.com/reference/java/util/IllegalFormatException` | If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the [Details](https://developer.android.com/reference/java/util/Formatter.html#detail) section of the formatter class specification. |

### readLine

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public String readLine ()
```

Reads a single line of text from the console.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/lang/String` | A string containing the line read from the console, not including any line-termination characters, or `null` if an end of stream has been reached. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOError` | If an I/O error occurs. |

### readPassword

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public char[] readPassword ()
```

Reads a password or passphrase from the console with echoing disabled

<br />

| Returns ||
|---|---|
| `char[]` | A character array containing the password or passphrase read from the console, not including any line-termination characters, or `null` if an end of stream has been reached. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOError` | If an I/O error occurs. |

### readPassword

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public char[] readPassword (String fmt, 
                Object... args)
```

Provides a formatted prompt, then reads a password or passphrase from
the console with echoing disabled.

<br />

| Parameters ||
|---|---|
| `fmt` | `String`: A format string as described in [Format string syntax](https://developer.android.com/reference/java/util/Formatter.html#syntax) for the prompt text. <br /> |
| `args` | `Object`: Arguments referenced by the format specifiers in the format string. If there are more arguments than format specifiers, the extra arguments are ignored. The maximum number of arguments is limited by the maximum dimension of a Java array as defined by The Java Virtual Machine Specification. <br /> |

| Returns ||
|---|---|
| `char[]` | A character array containing the password or passphrase read from the console, not including any line-termination characters, or `null` if an end of stream has been reached. <br /> |

| Throws ||
|---|---|
| `https://developer.android.com/reference/java/io/IOError` | If an I/O error occurs. |
| `https://developer.android.com/reference/java/util/IllegalFormatException` | If a format string contains an illegal syntax, a format specifier that is incompatible with the given arguments, insufficient arguments given the format string, or other illegal conditions. For specification of all possible formatting errors, see the [Details](https://developer.android.com/reference/java/util/Formatter.html#detail) section of the formatter class specification. |

### reader

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public Reader reader ()
```

Retrieves the unique `https://developer.android.com/reference/java/io/Reader` object associated
with this console.


This method is intended to be used by sophisticated applications, for
example, a `https://developer.android.com/reference/java/util/Scanner` object which utilizes the rich
parsing/scanning functionality provided by the `Scanner`:
>
> ```
> Console con = System.console();
> if (con != null) {
>     Scanner sc = new Scanner(con.reader());
>     ...
> }
> ```


For simple applications requiring only line-oriented reading, use
`https://developer.android.com/reference/java/io/Console#readLine()`.


The bulk read operations `https://developer.android.com/reference/java/io/Reader#read(char[])`,
`https://developer.android.com/reference/java/io/Reader#read(char[],%20int,%20int)` and
`https://developer.android.com/reference/java/io/Reader#read(java.nio.CharBuffer)`
on the returned object will not read in characters beyond the line
bound for each invocation, even if the destination buffer has space for
more characters. The `Reader`'s `read` methods may block if a
line bound has not been entered or reached on the console's input device.
A line bound is considered to be any one of a line feed (`'\n'`),
a carriage return (`'\r'`), a carriage return followed immediately
by a linefeed, or an end of stream.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/Reader` | The reader associated with this console <br /> |

### writer

Added in [API level 9](https://developer.android.com/guide/topics/manifest/uses-sdk-element#ApiLevels)

```
public PrintWriter writer ()
```

Retrieves the unique `https://developer.android.com/reference/java/io/PrintWriter` object
associated with this console.

<br />

| Returns ||
|---|---|
| `https://developer.android.com/reference/java/io/PrintWriter` | The printwriter associated with this console <br /> |