×

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

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

Dutch National Flag Problem in Java

Dutch National Flag (DNF) is a programming issue that Edsger Dijkstra put up. The white, red, and blue hues make up the Dutch flag. The goal is to haphazardly set...

6 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.

Method chaining in Java

Method chaining in Java is the act of calling a series of methods one after the other. The sole distinction between it and function Object () constructor chaining is the difference...

3 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.

Java Boolean logicalXor() Method

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

2 minutes read.

Multiple Inheritance Programs in Java

A component of the object-oriented notion known as multiple inheritances allows a class to inherit properties from multiple parent classes. When methods that have the same signature are present in...

4 minutes read.

How to compare three dates in Java?

While using the date and the time in Java, we occasionally have to compare the dates. Java does not compare dates the same way it compares the two numbers. Therefore,...

6 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Java Networking

Networking Networking is a way of communicating the devices. It is made up of various technologies like computers, switches, routers that are interconnected and share data among themselves. The two common network protocols: 1. TCP/IP TCP stands for...

10 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Java Variable

The variable is the basic unit of storage in a program. We define a variable using an identifier, a type, and an optional initializer in Java. In Java, variables must be...

4 minutes read.

How to Convert int to double in Java

How to Convert int to double in Java When two variables of different types are involved in the single expression, Java compiler uses built-in library function to convert the variable to...

2 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Difference Between Thread.start() and Thread.run()

In the Java programming language, the multi-threading concept consists of the start() and run() methods. Thread.start(): The thread's execution is initiated by invoking the start() method. The start() method operates two threads...

4 minutes read.