×

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and output generation. It has predefined classes used for performing input-output operations for files and input-output operations performed by a user through keyboard and console. To meet the requirements above, Java employs the concept of a stream. I/O operations become faster with this concept. It enables a program to write and read entire objects to streams. One of the main applications of Java I/O is to implement the concept of file handling in Java.

File Handling

As we mentioned, stream a lot of time in the above passage, so you might be wondering what stream is and how much it aids IO operations; so let's take a look at the concept of the stream. As the name implies, it is being used to implement two functions: reading and writing data in a file. It is used to apply various tasks on a file. It does not require any different package; the java.io package consists of all file classes and allows its users to work with different file formats.

Stream

A stream is defined as the order or progression of objects that are required to support a wide range of

objects. This idea was first presented in Java 8 and is still used today. Java Stream has the following features:

  • Instead of being a data structure, a stream accepts input from Collections, Arrays, and I/O Channels.
  • It does not alter the original data structure; instead, they deliver the result as specified by the pipelined methods.

Before delving into the various input and output streams available in Java, let's take a look at three standard or default streams that are also the most commonly used:

  1. System. In: It is used to read characters entered by the user via the keyboard.
  2. System. Out: It is used to display the output of a program on a display device which is fulfilled by implementing the print() method.
  3. System.err: It displays all of the error data generated by a program on a display device like a computer screen.

With the help of the code below, we can better grasp how System.in and System.out work.

JAVA I/O

We have used the Scanner class to take input in the above code.

Scanner sc=  new Scanner (System.in)

The above-highlighted line, which is used in the program at line number eight, is a Scanner class that can pass primitive types and strings using regular expressions where System.in represents the user’s keyboard.

String a = sc.next()

The above-highlighted line, which is used in the program at line number ten, takes the input from the user, which is in the configuration of String (in the above case).

We have used the print() method to print the input taken from the user and other text which we want to print. Following is the output of the code.

JAVA I/O

We have used two types of print: print (parameter) and println(parameter). When using the print() method, the cursor remains at the end of the text, while in the case of println, the cursor moves to the new line. Let's understand this with the following example.

JAVA I/O

First, we printed a space so that our output looked a bit nice.

Then we have printed 4 keywords in a row.

Inline no. 13, the cursor goes to a new line and print some space, and after line no.16, the cursor again goes to a new line and print "hello everyone". To understand more precisely, let's look at the output of the above code.

JAVA I/O

Types of Stream

Java provides two types of stream, which are explained below:

1. InputStream: Its purpose is to read data. Useful methods of InputStream are as follows.

  • Public abstract int read() throws an exception: It reads the next data byte.
  • Public it available() throws an exception: Its function is to estimate the byte used to read from the input stream.
  • Public void close() throws an exception: It closes the present input stream as the name suggests.

2. OutputStream: Its purpose is to write data. Useful methods of OutputStream are as follows.

  • Public void write(int) throws an exception: It is used to write a byte in the present output stream.
  • Public void writes ( a[] )throws an exception: As the parameter suggests, it writes an array of bytes to the present output stream.
  • Public void flush() throws an exception: It flushes the present output stream.
  • Public void close() throws an exception: As the name suggests, the above method closes the present output stream.

Features I/O Streams

I/O stream is a powerful idea that significantly simplifies I/O operations. The java.io package consists of all the necessary classes to perform I/O operations. Let's discuss different types and features I/O streams offer a java user.

  • Byte Streams: It is implemented to manage the input-output of raw binary data.
  • Character Streams: It is implemented to manage the input-output of character data.
  • Buffered Stream: It is used for optimizing input and output data.
  • Scanning and formatting: The program can perform two functions: reading and writing.
  • Data streams are implemented to manage binary input-output from primitive and String data types.
  • Object Streams: As the name suggests, it manages the Input-output of objects.

Java.io Classes

The most important Java.io classes are as follows:

  • BufferedInputStream
  • BufferedOutputStream
  • BufferedReader
  • BufferedWriter
  • ByteArrayInputStream
  • ByteArrayOutputStream
  • CharArrayReader
  • CharArrayWriter
  • Console
  • DataInputStream –
  • DataOutputStream
  • File
  • FileDescriptor
  • FileInputStream
  • FileOutputStream
  • FilePermission
  • FileReader and FileWriter
  • FilterInputStream
  • FilterOutputStream
  • FilterReader
  • FilterWriter
  • InputStream
  • InputStreamReader
  • LineNumberInputStream
  • LineNumberReader
  • ObjectInputStream
  • ObjectInputStream.GetField
  • ObjectOutputStream
  • ObjectOutputStream.PutField
  • ObjectStreamClass
  • ObjectStreamField
  • OutputStream
  • OutputStreamWriter
  • PipedInputStream
  • PipedOutputStream
  • PipedReader
  • PipedWriter
  • PrintStream
  • PrintWriter
  • PushbackInputStream
  • PushbackReader
  • RandomAccessFile
  • Reader
  • SequenceInputStream
  • SerializablePermission
  • StreamTokenizer
  • StringBufferInputStream
  • StringReader
  • StringWriter
  • Writer
  • ZipInputStream class in Java
  • ZipEntry class in Java
  • JarEntry class in Java
  • ZipOutputStream class in Java
  • Zip.InflaterInputStream class in Java
  • Zip.DeflaterInputStream class in Java
  • Zip.DeflaterOutputStream class in Java

Related Topics

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

Java Integer sum() method

The sum() method of Java Integer class add the two specified integers values. It returns the same result as given by + operator. Syntax public static int sum (int a, int b)  Parameters The...

1 minute read.

Object Oriented Programming (OOPs)

Since the dawn of earth, we have kept on evolving. The need for evaluation comes with the aim of improving the existing systems when something inappropriate is found for the...

5 minutes read.

Java DatagramSocket and Java DatagramPacket

Datagrams TCP/IP style networking specifies a serialized, predictable, and reliable stream of data in the form of a packet. Servers and clients communicate through a reliable channel, such as TCP socket, have a dedicated...

6 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Composition in Java

Composition Java uses the composition method to implement a has-a connection. Composition allows us to reuse code in the same way that Java inheritance does. The "is-a" relationship is implemented using the...

3 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

How to run java program in ubuntu

To run the java programming in ubuntu, we need to follow several steps: Let's see the process. Step1: Install the Java compiler or JDK to the system. To run the java program, we...

3 minutes read.

Multithreading in Java

Multithreading is a specialized form of multitasking. It is responsible for executing more than one task at a time of a single program, and each task is a separate thread. A program...

8 minutes read.

How to Convert String to enum in Java?

In this article, we shall gain the complete knowledge about how to convert the string to enum in Java. The complete process that happens in the approach shall be discussed...

3 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Java Boolean logicalOr() Method

The logicalOr() method of Java Boolean class returns the result of implementing logical OR operation on the specified Boolean operands. Syntax: public static boolean logicalOr (boolean a, boolean b) Parameters: The parameters ‘a’ and...

2 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java String format() method

format() method returns a formatted String based on the given locale,specified format and arguments. Syntax: public static String format(String format , Object… args) Parameter: locale : It specifies locale value to be applied on...

2 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

Java InputStreamReader

What is InputStreamReader?An InputStreamReader is a converter between byte and character streams: It reads bytes and converts them to characters with the help of a charset. The charset it uses can...

4 minutes read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Flag Pattern in Java

The flag pattern in Java can be printed, which will be covered in this part. Given how difficult they are to code, flag patterns are rarely asked by interviewers. We separate...

2 minutes read.