Advanced Input Output in Java

In this article we will discuss “Advanced Input Output in Java” in detail. We will also explain an example to showcase the operation of Advanced Input Output in Java.

Introduction

Java predominantly relies on various streams to perform input and output operations. These streams are designed such that it is compatible with data of all types including objects, characters, files, etc. The workflow of any program involving input and output operations are as follows:

An Input stream is received from a source. This stream is fed into the Java Application. The application produces an output stream after computation. This stream is displayed or written to the console.

Standard Streams used in Java

To perform the above-mentioned cycle, Java requires three standard streams:

  • in – This is the standard input stream. It reads input made using keyboards or other input devices.
  • out – This is the standard output stream. The output of a program is displayed using this stream on output devices like a monitor. This stream has various print functions that it can be attached to namely print(), println(), and printf(). While print() displays the text and keeps the cursor placed on the same line, with println(), the cursor is moved to the next line such that the next output will appear on a new line. printf() allows for more than one argument to be printed at the same time.
  • err – This is the standard error stream. Whenever a program encounters an error, this stream is summoned to display it on the screen.

Example

import java.io.*;

class Sample {
public static void main (String args [])
{
System.out.print(“Hello World   ”);
System.out.println(“Hello World”);
System.out.print(“Hello World”);
}
}

 

OUTPUT

Hello World  Hello World
Hello World

Advanced I/O in Java

JDK 1.4 has introduced NIO (New Input Output) in its java.nio package to support high-performance input and output program requirements. NIO acts not as a replacement to the existing I/O streams but as a support system to strengthen a program’s responsiveness. NIO Buffers are used to facilitate this process.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.