Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
125 views

Java IO: Input-Output in Java With Examples

The document discusses input and output in Java. It describes 3 standard streams for input/output: standard output, standard error, and standard input. It then provides examples of using print() and println() methods to write to standard output. The rest of the document discusses different methods for reading input from the console in Java, including using BufferedReader, Console class, and Scanner class.

Uploaded by

gopi9966957145
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
125 views

Java IO: Input-Output in Java With Examples

The document discusses input and output in Java. It describes 3 standard streams for input/output: standard output, standard error, and standard input. It then provides examples of using print() and println() methods to write to standard output. The rest of the document discusses different methods for reading input from the console in Java, including using BufferedReader, Console class, and Scanner class.

Uploaded by

gopi9966957145
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Java IO: Input-output in Java with Examples

Java brings various Streams with its I/O package that helps the user to perform all
the input-output operations. These streams support all the types of objects, data-
types, characters, files etc to fully execute the I/O operations.

Before exploring various input and output streams let’s look at 3 standard or
default streams that Java has to provide which are also most common in use:
Methods to write data on to the Console

 print(): This method in Java is used to display a text on the console. This


text is passed as the parameter to this method in the form of String. This
method prints the text on the console and the cursor remains at the end of
the text at the console. The next printing takes place from just here.
Syntax:

System.out.print(parameter);

Example:

// Java code to illustrate print()

import java.io.*;

  class Demo_print {

    public static void main(String[] args)

    {

          // using print()

        // all are printed in the

        // same line

        System.out.print("GfG! ");

        System.out.print("GfG! ");

        System.out.print("GfG! ");

    }

}
Output:

GfG! GfG! GfG!


 println(): This method in Java is also used to display a text on the console. It
prints the text on the console and the cursor moves to the start of the next
line at the console. The next printing takes place from the next line.
Syntax:

System.out.println(parameter);

Example:

// Java code to illustrate println()

Import java.io.*;

  class Demo_print {

    public static void main(String[] args)

    {

          // using println()

        // all are printed in the

        // different line

        System.out.println("GfG! ");

        System.out.println("GfG! ");

        System.out.println("GfG! ");

    }

}
Output:

GfG!

GfG!

GfG!
Methods to Read Input from the Console

The input stream allows few 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 (System.in) 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 java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

public class Main {

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

          

        BufferedReader reader = 

                   new BufferedReader(new InputStreamReader(System.in));

      System.out.println("Enter the input string");

        String name = reader.readLine();

       System.out.println("You entered: " + name);        

    }

}
Output:

In the above program, we have declared an object of BufferedReader class


initialized to System.in 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 “System.console” 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 System.console class.

public class Main

    public static void main(String[] args) 

    {        

        System.out.println("Enter the input string");

        String name = System.console().readLine();

        System.out.println("You entered: " + name);        

    }

}
Output:

Using System.console 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 System.out.printf ().

Although this is the preferred way of reading input data, note that the class
System.console 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 java.util.Scanner;

class Main

    public static void main(String args[])

    {

        Scanner myscan = new Scanner(System.in);


   

        System.out.println("Enter the input:");

        String mystr = myscan.nextLine();

        System.out.println("You entered a string:"+mystr);

        System.out.println("Enter Next input:"); 

        int num = myscan.nextInt(); 

        System.out.println("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