Java – Input/Output example

PREV     NEXT

  • A simple java program takes an input, process the input and produces the output as result.
  • Input can be taken from the keyboard, from a file, from main memory or from elsewhere. Now if the input is defined inside the program itself, then running it anytime would present us the same output.
  • In Order for an effective use of the program the input has to be received from outside of the program and hence IO functions are defined.

Streams:

  • Streams are used to handle input/output operations.
  • Stream is like a pipe which carries data from one end to the other. The end points are nothing but the source and destination of the data to be read/written.

Getting input from console and writing in a file.Input from Console

Streams are classified into two types, which are the parent class of all the subclasses of other streams.

  • InputStream – Superclass of all classes used to read the input.
  • OutputStream – Superclass of all classes used to write the output.

There are 3 fields in System class which represents some stream.


  1. in – Represents the standard input device
  2. out – Represents the standard output device
  3. err – same as System.out (Errors are displayed using this field.)
  • System.in field represents the InputStream object. Both System.out and System.err fields represents the PrintStream object.
  • To accept the data from the input device, we need to connect the System.in (representing the basic input device) to an input stream.

Example:

InputStreamReader object = new InputStreamReader(System.in);

Based on how we are going to read/write the data, we have 2 categories in streams.Streams

  • Byte Streams – ASCII 8 bits (each character requires 1 byte)
  • Character Streams – Unicode 16 bits (each character requires 2 byte)

ByteStreams:

  • Byte streams are used to read/write input/output of 8 bit bytes.All kind of data (text,image,audio,video) can be read/written using byte streams.

The following diagrams shows some of the subclasses of InputStream and Output Streams which falls under the category of byte streams.


Byte Streams for reading the input data:

Byte Streams

Byte Streams for writing the output data:Byte Stream Output

 

 

 

 

 

 

  • There are 9 subclasses of InputStream defined under the Java SE 8 version.
  • AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream.

Reading a character from the console:

COMPILE & RUN

Output:

Enter a character:a

You entered the character:a

Enter a string:

The program terminates without getting the string value (st). This is because while entering input in the first line, we type a character(Here ‘a’) and hit enter. The enter is taken as value ‘\n’ and assigned to the next input value (i.e. for  st = br.readLine(); ) and hence \n is assigned to st and the same is printed which is a blank line. Thus the above output is thrown by the program.

Reading Other Data Types from the keyboard:

The readLine() method gets the input from the console as string, which can be later converted to any type using the parse method of that data type.

        System.out.println(“Enter an integer:”);

int n = Integer.parseInt(br.readLine());

System.out.println(“Enter a float value:”);

float f = Float.parseFloat(br.readLine());

//Similar conversion can be made for double, byte,boolean,long,short values

We can also tokenize the given string and store the tokens into separate variables. This can be done using the StringTokenizer class with the help of nextToken() method.

PREV     NEXT

Share this website !!!
Facebook Twitter Linkedin