import java.io.*;

/**
 * Read and write to console using all different techniques 
 */

public class IOx
{
   public static void main(String[] args)
   {
      try
      {
         //byte stream output unbuffered ---------------------------------------------
         {
            System.out.println("\n----- 8 bit byte I/O -------");

            System.out.print("\n Enter a byte: ");          
            int i = System.in.read();           	//read into lowest byte of int          
            System.out.write(i);                	//write from lowest byte of int

            System.in.read();                   	//get rid of carriage return
            System.in.read();                   	//get rid of line feed

            System.out.print("\n Write a byte: "); 
            byte b = 'x';                           //create a byte
            System.out.write(b);                    //write a single byte


            byte[ ] array = new byte[250];          //define a byte array 

            System.out.print("\n Enter a few bytes: ");     
            int num = System.in.read(array);        //read array, get num byte read
            System.out.write(array,0,num);          //write array from 0 to num

            System.out.print("\n Write a few bytes: ");
            String s = "this is some text";
            array = s.getBytes();                   //convert string to byte array
            System.out.write(array);

        //byte stream output buffered ----------------------------------------------------

            BufferedInputStream  keyboardBuf = new BufferedInputStream(System.in);
            BufferedOutputStream screenBuf   = new BufferedOutputStream(System.out);

            System.out.print("\n\n Enter a few bytes: (buffered) ");   	//read/write buffered
            num = keyboardBuf.read(array);                 				//read buffered  
            screenBuf.write(array,0,num);                  				//write buffered 
            screenBuf.flush();                              			//flush the buffer
         }


         //char (16bit) stream output unbuffered -------------------------------------------
         {
            System.out.println();
            System.out.println("\n----- 16 bit char I/O -------");

            InputStreamReader  keyboard = new InputStreamReader(System.in);
            OutputStreamWriter screen   = new OutputStreamWriter(System.out);

            System.out.print("\n Enter a char: ");      //read/write a single int
            int j = keyboard.read();                    //read into lowest 2 bytes of int
            screen.write(j);                            //write from lowest 2 bytes of int
            screen.flush();                             //flush to output

            keyboard.read();                			//get rid of carriage return
            keyboard.read();                			//get rid of line feed

            System.out.print("\n Write a char: ");
            char c = 'x';                   			//create a char
            screen.write(c);                			//write a single char
            screen.flush();                 			//flush to output


            char[ ] array = new char[250];              //define a character array 

            System.out.print("\n Enter a few chars: "); //read/write a char array
            int num = keyboard.read(array);             //read array, get num char read
            screen.write(array,0,num);                  //write array from 0 to num
            screen.flush();                             //flush to output

            System.out.print("\n Write a few chars: ");
            String s = "some char text";
            array = s.toCharArray();                    //convert string to char array
            screen.write(array);
            screen.flush();

            System.out.print("\n Write a string...: ");     //write a string at a time
            String s2 = "some string";                      //create a string
            screen.write(s2 + "\n");                        //write a string + \n
            screen.flush();

         //char (16bit) stream output buffered ----------------------------------------------

            BufferedReader keyboardBuf = new BufferedReader(keyboard);
            PrintWriter    screenBuf   = new PrintWriter(screen);

            System.out.print("\n Enter a string: (buffered) "); //read/write a string
            String s3 = keyboardBuf.readLine();                 //read a string
            screenBuf.print(s3 + "\n" );                        //write a string + \n
            screenBuf.flush();                                  //flush the buffer
         }
      }

      catch (IOException e)
      {
         System.out.println(e);
      }
   }
}