Tuesday, 16 August 2016

Java Tutorial : Java IO (InputStreamReader-Read from keyboard-while loop)

InputStreamReaderDemo.java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class InputStreamReaderDemo
{

    public static void main(String[] args) throws IOException
    {
        InputStreamReaderDemo inputStreamReaderDemo = new InputStreamReaderDemo();
        inputStreamReaderDemo.readData();
    }

    private void readData() throws IOException
    {
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;

        try
        {
            inputStreamReader = new InputStreamReader(System.in);
            /*
             * We are connecting the BufferedReader stream
             * with the InputStreamReader stream for reading
             * the line by line data from the keyboard.
             */
            bufferedReader = new BufferedReader(inputStreamReader);

            String data = "";
            while (!data.equals("stop"))
            {
                System.out.print("Enter data : ");
                data = bufferedReader.readLine();
                System.out.println("data is : " + data);
            }

        }
        finally
        {
            if (inputStreamReader != null)
            {
                inputStreamReader.close();
            }
            if (bufferedReader != null)
            {
                bufferedReader.close();
            }
        }

    }

}
Output
Enter data : hello
data is : hello
Enter data : bye
data is : bye
Enter data : welcome
data is : welcome
Enter data : stop
data is : stop
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_InputStreamReader_Read_while_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_InputStreamReader_Read_while_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/ebaa49adbc935437412c3f95a1070f272f5231dd/BasicJava/JavaIODemo_InputStreamReader_Read_while_App/?at=master
See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge