1 of 25

Java I/O Programming

2 of 25

Java I/O (Input and Output) is used to process the input and produce the output.

Java uses the concept of a stream to make I/O operation fast. The java.io package contains all the classes required for input and output operations.

Java brings various Streams with its I/O package that helps the user to perform all the input-output operations. These streams support all the types of objects, data-types, characters, files etc to fully execute the I/O operations.

Stream

A stream can be defined as a sequence of data. In Java, a stream is composed of bytes. There are two kinds of Streams −

  • InPutStream − The InputStream is used to read data from a source.
  • OutPutStream − The OutputStream is used for writing data to a destination.

3 of 25

Types of Streams:

Depending on the type of operations, streams can be divided into two primary classes:

  • Input Stream: These streams are used to read data that must be taken as an input from a source array or file or any peripheral device. For eg., FileInputStream, BufferedInputStream, ByteArrayInputStream etc.
  • Output Stream: These streams are used to write data as outputs into an array or file or monitor or any output peripheral device. For eg., FileOutputStream, BufferedOutputStream, ByteArrayOutputStream etc.

Depending on the types of file(the data a stream holds), Streams can be divided into two primary classes: ByteStream,CharacterStream.

4 of 25

5 of 25

ByteStream: This is used to process data byte by byte (8 bits).

  • Byte stream is used to read and write a single byte (8 bits) of data.
  • All byte stream classes are derived from base abstract classes called InputStream and OutputStream.

6 of 25

CharacterStream: In Java, characters are stored using Unicode conventions.

  • Character stream automatically allows us to read/write data character by character.
  • Character stream is used to read and write a single character of data.
  • All the character stream classes are derived from base abstract classes Reader and Writer.

7 of 25

FileInputStream: This class reads the data from a specific file (byte by byte). It is usually used to read the contents of a file with raw bytes, such as images.

  • First of all, you need to instantiate this class by passing a String variable(path of file) or a File object, representing the path of the file to be read.

Then read the contents of the specified file using either of the variants of read() method −

    • int read() − This simply reads data from the current InputStream and returns the read data byte by byte (in integer format).This method returns -1 if the end of the file is reached.
    • int read(byte[] b) − This method accepts a byte array as parameter and reads the contents of the current InputStream, to the given array. This method returns an integer representing the total number of bytes or, -1 if the end of the file is reached.
    • int read(byte[] b, int off, int len) − This method accepts a byte array, its offset (int) and, its length (int) as parameters and reads the contents of the current InputStream, to the given array. This method returns an integer representing the total number of bytes or, -1 if the end of the file is reached.

8 of 25

File class:

File handling is an important part of any application.Java has several methods for creating, reading, updating, and deleting files. The File class from the java.io package, allows us to work with files. To use the File class, create an object of the class, and specify the filename or directory name

import java.io.File; // Import the File class

File myObj = new File("filename.txt"); // Specify the filename

9 of 25

To create a file in Java, you can use the createNewFile() method. This method returns a boolean value: true if the file was successfully created, and false if the file already exists. Note that the method is enclosed in a try...catch block. This is necessary because it throws an IOException if an error occurs (if the file cannot be created for some reason):

10 of 25

To create a file in a specific directory (requires permission), specify the path of the file and use double backslashes to escape the "\" character (for Windows).On Mac and Linux you can just write the path, like:

/Users/name/filename.txt

File myObj = new File("C:\\Users\\MyName\\filename.txt");

11 of 25

There are many available classes in the Java API that can be used to read and write files in Java:FileReader, BufferedReader, Files, Scanner, FileInputStream, FileWriter, BufferedWriter, FileOutputStream, etc.

Which one to use depends on the data you're working with whether you need to read bytes or characters, and the size of the file/lines etc.Following is the example for reading data from the file using Scanner class:

12 of 25

13 of 25

close() Method

To close the file input stream, we can use the close() method. Once the close() method is called, we cannot use the input stream to read data.

FileInputStream

14 of 25

available() Method

To get the number of available bytes, we can use the available() method.

skip() Method: To discard and skip the specified number of bytes, we can use the skip() method.

15 of 25

FileOutputStream class

The FileOutputStream class in Java is used to write data to a file in the form of bytes. It belongs to the java.io package and is often used for writing binary data like images, audio files, or simple text (in byte form).

16 of 25

17 of 25

Java DataInputStream Class: this class allows an application to read primitive data from the input stream in a machine- independent way.

18 of 25

import java.io.*;

public class InputExample

{

public static void main(String[] args) throws IOException

{

FileInputStream file = new FileInputStream("D:\\testout.txt");

DataInputStream data = new DataInputStream(file);

int num = data.readInt();

System.out.println("Number read: " + num);

data.close();

System.out.println("Success...");

}

}

Output:�Number read: 65�Success...

19 of 25

Java DataOutputStream Class: Java DataOutputStream class allows an application to write primitive Java data types to the output stream in a machine-independent way.

20 of 25

21 of 25

Java BufferedInputStream Class: Java BufferedInputStream class is used to read information from stream. It internally uses buffer mechanism to make the performance fast.

When the bytes from the stream are skipped or read, the internal buffer automatically refilled from the contained input stream, many bytes at a time.When a BufferedInputStream is created, an internal buffer array is created.

22 of 25

23 of 25

24 of 25

Java BufferedOutputStream Class:

  • Java BufferedOutputStream class is used for buffering an output stream.
  • It internally uses buffer to store data.
  • It adds more efficiency than to write data directly into a stream. So, it makes the performance fast. The flush() flushes the data of one stream and send it into another.
  • It is required if you have connected the one stream with another.

25 of 25

Thank You