Input Output Streams in Java

In this tutorial, We will learn about the classes in the Java IO (java.io) package.

Let’s first understand the terms “input” and “output”. The input of one part of an application is often the output of another.

Java I/O is used to process the input and produce the output based on the input. Java uses the concept of the stream to make I/O operation.

In general, a stream means the continuous flow of data. Streams are the clean way to deal with input/output without having every part of your code understand the physical.

 

Streams  in Java:

Java IO streams are flows of data you can either read from or write to. Streams are typically connected to a data source, or data destination, like a file, network connection etc.

A stream has no concept of an index of the read or written data like an array does. Nor can you typically move forth and back in a stream, like you can in an array. A stream is just a continuous flow of data.

Java encapsulates Stream under java.io package. Java defines two types of streams. They are –

  • Byte Stream : It provides a convenient means for handling input and output of byte.
  • Character Stream : It provides a convenient means for handling input and output of characters. Character stream uses Unicode and therefore can be internationalized.

 

Java IO

We will learn about all these streams in coming tutorials in details.

Streams System.in, System.out, and System.err are common sources or destinations of data. All these streams are attached to the console. Most commonly used is probably System.out for writing output to the console from console programs.

These 3 streams are initialized automatically by the Java runtime when a JVM starts up, so you don’t have to instantiate any streams yourself.

 

System.in Stream:

System.in is a Standard InputStream, which is typically connected to keyboard input of console programs. System.in is not used as often since data is commonly passed to a command-line Java application via command line arguments or configuration files. In applications with GUI, the input to the application is given via the GUI. This is a separate input mechanism from Java IO.

 

System.out Stream:

System.out is a Standard PrintStream. System.out normally outputs the data you write to it to the console. This is often used from console-only programs like command line tools. This is also often used to debug statements of from a program.

 

System.err Stream:

System.err is a Standard Error PrintStream. System.err works like System.out except it is normally only used to output error texts. Some programs (like Eclipse) will show the output to System.err in red text, to make it more obvious that it is error text.

Selenium Tutorial:

If you liked this video, then please subscribe to our YouTube Channel for more video tutorials.

 

System.out + System.err Example

Let’s see the simple example of system.out and system.err streams.

Output:

You know the output of the above code. output will be –

File opening failed:
java.io.FileNotFoundException: c:\data\… (The system cannot find the file specified)

We have seen the streams and also seen the basic and auto-invoked streams. In coming tutorials, we will see all topic in deep thought.

0 Comment

Leave a Reply