0% found this document useful (0 votes)
13 views6 pages

Java I/O Streams and Input Methods

The document provides an overview of Java I/O, detailing the use of the 'java.io' package and the concept of streams for input and output operations. It explains standard I/O streams such as System.in for input, System.out for output, and System.err for error handling, along with methods to read input from the console using BufferedReader, Console, and Scanner classes. Examples are included to demonstrate the functionality of these streams and classes in Java programming.

Uploaded by

Rupali Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Java I/O Streams and Input Methods

The document provides an overview of Java I/O, detailing the use of the 'java.io' package and the concept of streams for input and output operations. It explains standard I/O streams such as System.in for input, System.out for output, and System.err for error handling, along with methods to read input from the console using BufferedReader, Console, and Scanner classes. Examples are included to demonstrate the functionality of these streams and classes in Java programming.

Uploaded by

Rupali Shinde
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Java I/O (Input-Output) is a standard mechanism that processes the input and generates

the output. The package “[Link]” contains the methods to perform all the input and output
operations.

To perform I/O operations faster, Java uses the concept of streams. A stream can be
defined as a sequence of data consisting of bytes.

Let’s learn more about Java I/O streams!!

Standard I/O Streams In Java

Java language offers access to system resources, standard input-output devices, etc. using
a “System” class. This class implements a system-dependent programming interface to
access various resources.

The System class belongs to the “[Link]” package of Java. Apart from providing standard
I/O streams, System class also provides access to environment variables, external
variables, loading files and libraries, and also a utility method arrayCopy for copying part of
an array.

From Input-Output point of view, the System class offers the following streams:

#1) Standard Input Stream ([Link])

The input stream provided by System class, [Link] is used to read the input data from a
standard input device like a keyboard.

The stream remains open and is ready to read the data supplied by the user to the standard
input device.

The below example demonstrates the [Link] function to read a single integer
digit.

public class Main {


public static void main(String args[]) throws
[Link] {
int ch;
[Link]("Enter the character to be displayed :
");

ch = [Link]();
[Link]("You entered : " + (char)ch);

}
}
Output:

#2) Standard Output Stream ([Link])

The [Link] interface of the System class is used to write the program output to the
standard output device like the monitor. In most cases, the [Link] interface writes the
command output to the standard output device.

It uses three methods from the “PrintStream” class as the standard output derives from this
class.

These methods are:

1. print
2. println
3. write

The methods “print” and “println” have the same functionality except for a single difference
that the println method appends a newline character (\n) to the output.

The write method is not used frequently except in cases where non-ASCII data is to be
displayed.

The following example demonstrates the [Link] stream.

public class Main {


public static void main(String args[]) throws
[Link] {

String stringType = "Java Tutorial Series";


char[] charTypeArray = { 'a', 'e', 'i', 'o', 'u' };
boolean booleanType = true;
int integerType = 10;
double doubleType = [Link];
long longType = Long.MAX_VALUE;
float floatType = Float.MIN_VALUE;
[Link]("String::" + stringType);
[Link]("Character::");
[Link](charTypeArray);
[Link]("Boolean::" + booleanType);
[Link]("Integer::" + integerType);
[Link]("Double::" + doubleType);
[Link]("Long::" + longType);
[Link]("Float::" + floatType);
}
}
Output:

The above program shows the “print” and “println” functions used with [Link] interface.
Here we have defined variables of different data types and display each of them using
[Link] interface.

#3) Standard Error Stream ([Link])

The standard error stream, [Link] is used to display errors if any during the execution
of the program.

Like [Link] stream, the error stream also supports the three methods of PrintStream
class, print, println and writes.

Methods To Read Input From The Console

Apart from the input stream described above, there are few more methods using which we
can read input data from the console in Java.

These methods are discussed below.


#1) Class BufferedReader

The class BufferedReader was first introduced in JDK 1.0 and is the classical method of
reading input data from the console.

The input stream ([Link]) is wrapped inside the class InputStreamReader which is in
turn wrapped in BufferedReader.

The following program shows the usage of the BufferedReader class to read input
data from the user.

import [Link];
import [Link];
import [Link];
public class Main {
public static void main(String[] args) throws IOException {

BufferedReader reader =
new BufferedReader(new
InputStreamReader([Link]));

[Link]("Enter the input string");


String name = [Link]();

[Link]("You entered: " + name);


}
}
Output:

In the above program, we have declared an object of the BufferedReader class initialized to
[Link] stream. Using this object, we read an entire line of input.

As you can see, you can read the entire buffered data making this functionality very
efficient. The only drawback is the cryptic code that might be hard to remember every time.

#2) Console Class

The class “[Link]” can be used to read input from the console. This is used to
especially read input characters like a password from the command line.
The method of reading input data using the console class is currently the most efficient and
preferred method in Java.

The following program demonstrates the [Link] class.

public class Main


{
public static void main(String[] args)
{
[Link]("Enter the input string");
String name = [Link]().readLine();

[Link]("You entered: " + name);


}
}
Output:

Using [Link] class, you can read the input characters without echoing the
characters. Hence this method is more useful for reading passwords. Secondly, you can
also use format strings to format the input data, similar to the ones used in [Link]
().

Although this is the preferred way of reading input data, note that the class [Link]
cannot be used with an Interactive Java environment like IDEs.

#3) Scanner

Using a scanner class to read input data is probably the fastest and preferred method. The
scanner is mostly used for parsing the data types including primitive types and strings. But it
can also be used to read the input data and parse it using tokenized input.

The scanner class uses regular expressions for this purpose.

The following program reads the input data from the user using the scanner class.

import [Link];

class Main
{
public static void main(String args[])
{
Scanner myscan = new Scanner([Link]);

[Link]("Enter the input:");


String mystr = [Link]();
[Link]("You entered a string:"+mystr);
[Link]("Enter Next input:");
int num = [Link]();
[Link]("You entered an integer:"+num);
}
}
Output:

In the above program, we have used the scanner class to read the string and integer
data.

You might also like