Java-Streams


/*********************************************************************************
 * This program uses the Keyboard as the input device to accept data from the     *
 * MS-DOS prompt (console). It gets a character from the keyboard, and prints the *
 * ASCII value of the character inputted as well as the character itself.         *
 *********************************************************************************/

import java.io.*; //for IOException & read() throws and java.io.IOException

public class TestKeyboardIO1 {
  public static void main(String[] args) throws IOException {
    /*System.out.println("Type in a character and press ENTER.");
    System.out.println("------------------------------------");

           // reads a byte from the keyboard Buffer.
           // Returns an int.
    int data = System.in.read();
          
    System.out.println("ASCII  :  " + data);
    System.out.println("Character :  " + (char) data);*/
          

    /*System.out.println("Type in a character and press ENTER.");
    System.out.println("------------------------------------");

    int data = System.in.read();
    System.out.println("ASCII  :  " + data);
    System.out.println("Character :  " + (char) data);

           data = System.in.read();
    System.out.println("ASCII  :  " + data);
    System.out.println("Character :  " + (char) data);

    data = System.in.read();
    System.out.println("ASCII  :  " + data);
    System.out.println("Character :  " + (char) data);

    data = System.in.read();
    System.out.println("ASCII  :  " + data);
    System.out.println("Character :  " + (char) data);*/


    /*System.out.println("Type in a character and press ENTER.");
    System.out.println("------------------------------------");

    int data = System.in.read();
    System.out.println("ASCII  :  " + data);
    System.out.println("Character :  " + (char) data);

           System.in.skip(System.in.available()); // empty the inputstream

    data = System.in.read();
    System.out.println("ASCII  :  " + data);
    System.out.println("Character :  " + (char) data);*/


           /*byte[] buffer = new byte[10];
           int bytesRead = System.in.read(buffer);
           System.out.println("Actual bytes read: " + bytesRead);
          
           //System.out.println(buffer);// invalid usage

           for (int i = 0; i < buffer.length; i++) {
                System.out.println(buffer[i] + "  " + (char) buffer[i]);
           }

           System.out.println("Converted to string: " +
                new String(buffer));*/

           /*byte[] buffer = new byte[10];
           //System.in.read(buffer, 0, 5);
           System.in.read(buffer, 2, 6);
          
           for(int i = 0; i < buffer.length; i++) {
                System.out.println(buffer[i] + "  " +
                (char) buffer[i]);
           }

           System.out.println(new String(buffer));*/
          



           /*byte b[] = "Hello World".getBytes();
           for(int i = 0; i < b.length; i++)
                System.out.println(b[i] + "  " + (char) b[i]);
           */

    System.out.println(System.in.getClass().getName()); // java.io.BufferedInputStream
    System.out.println(System.out.getClass().getName()); // java.io.PrintStream
          
  }
}
-----------------------------------------------------------------------------------
/********************************************************************************
 * This program uses the Keyboard as the input device to accept data from the    *
 * MS-DOS prompt (console). It gets a string from the keyboard and prints it.    *
 ********************************************************************************/

import java.io.*; //for IOException, InputStream, InputStreamReader, BufferedReader

public class TestKeyboardIO2 {
  public static void main(String args[]) throws IOException {
    /* converting from byte stream (InputStream subclass) to character stream (Reader subclass)*/
    /*InputStreamReader in_stream_reader =
           new InputStreamReader(System.in);*/

    /* converting from Reader subclass (in_stream_reader) to a BufferedReader object*/
    /*BufferedReader buf_reader =
           new BufferedReader(in_stream_reader);*/

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


    System.out.println("Type in a sentence and press ENTER to terminate.");
    System.out.println("------------------------------------------------");
    String data1 = buf_reader.readLine();// method to read a line.
    //String data1 = new BufferedReader(
           //   new InputStreamReader(System.in)).readLine();

    System.out.println();

    System.out.println("Type in a sentence and press ENTER to terminate.");
    System.out.println("------------------------------------------------");
    String data2 = buf_reader.readLine();// method to read a line.
    //String data2 = new BufferedReader(
           //   new InputStreamReader(System.in)).readLine();
   
           System.out.println();

    System.out.println("1st sentence: " + data1);
    System.out.println("2nd sentence: " + data2);
  }
}

Post a Comment

0 Comments