Java I/O Programming
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 −
Types of Streams:
Depending on the type of operations, streams can be divided into two primary classes:
Depending on the types of file(the data a stream holds), Streams can be divided into two primary classes: ByteStream,CharacterStream.
ByteStream: This is used to process data byte by byte (8 bits).
CharacterStream: In Java, characters are stored using Unicode conventions.
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.
Then read the contents of the specified file using either of the variants of read() method −
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
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):
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");
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:
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
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.
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).
Java DataInputStream Class: this class allows an application to read primitive data from the input stream in a machine- independent way.
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...
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.
Java BufferedOutputStream Class:
Thank You