In Java, streams are the sequence of data that are read from the source and written to the destination. Streams of two different types
FileInputStream is a class which is available in java.io package. FileInputStream is used to read data from file in the form of bytes. This extends InputStream abstract class.
FileInputStream input = new FileInputStream(stringPath);
FileInputStream input = new FileInputStream(File fileObject);
import java.io.FileInputStream;
public class Main {
public static void main(String args[]) {
try {
FileInputStream input = new FileInputStream("input.txt");
System.out.println("Data in the file: ");
// Reads the first byte
int i = input.read();
while(i != -1) {
System.out.print((char)i);
// Reads next byte from the file
i = input.read();
}
input.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
The BufferedInputStream class of the java.io package is used with other input streams to read the data (in bytes) more efficiently.
It extends the InputStream abstract class.
The BufferedInputStream maintains an internal buffer of 8192 bytes.
During the read operation in BufferedInputStream, a chunk of bytes is read from the disk and stored in the
internal buffer. And from the internal buffer bytes are read individually. Hence, the number of communication
to the disk is reduced. This is why reading bytes is faster using the BufferedInputStream.
In order to create a BufferedInputStream, we must import the java.io.BufferedInputStream package first. Once we import the package here is how we can create the input stream.
// Creates a FileInputStream
FileInputStream file = new FileInputStream(String path);
// Creates a BufferedInputStream
BufferedInputStream buffer = new BufferInputStream(file);
// Creates a BufferedInputStream with specified size internal buffer
BufferedInputStream buffer = new BufferInputStream(file, int size);
import java.io.BufferedInputStream;
import java.io.FileInputStream;
class Main {
public static void main(String[] args) {
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("input.txt");
// Creates a BufferedInputStream
BufferedInputStream input = new BufferedInputStream(file);
// Reads first byte from file
int i = input .read();
while (i != -1) {
System.out.print((char) i);
// Reads next byte from the file
i = input.read();
}
input.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
The FileOutputStream class of the java.io package can be used to write data (in bytes) to the files. It extends the OutputStream abstract class.
In order to create a file output stream, we must import the java.io.FileOutputStream package first. Once we import the package, here is how we can create a file output stream in Java.
// Including the boolean parameter
FileOutputStream output = new FileOutputStream(String path, boolean value);
// Not including the boolean parameter
FileOutputStream output = new FileOutputStream(String path);
FileOutputStream output = new FileOutputStream(File fileObject);
import java.io.FileOutputStream;
public class Main {
public static void main(String[] args) {
String data = "This is a line of text inside the file.";
try {
FileOutputStream output = new FileOutputStream("output.txt");
byte[] array = data.getBytes();
// Writes byte to the file
output.write(array);
output.close();
}
catch(Exception e) {
e.getStackTrace();
}
}
}
The BufferedOutputStream class of the java.io package is used with other output streams to write the data (in bytes) more efficiently. It extends the OutputStream abstract class. The BufferedOutputStream maintains an internal buffer of 8192 bytes. During the write operation, the bytes are written to the internal buffer instead of the disk. Once the buffer is filled or the stream is closed, the whole buffer is written to the disk. Hence, the number of communication to the disk is reduced. This is why writing bytes is faster using BufferedOutputStream.
In order to create a BufferedOutputStream, we must import the java.io.BufferedOutputStream package first. Once we import the package here is how we can create the output stream.
// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream(String path);
// Creates a BufferedOutputStream
BufferedOutputStream buffer = new BufferOutputStream(file);
// Creates a BufferedOutputStream with specified size internal buffer
BufferedOutputStream buffer = new BufferOutputStream(file, int size);
import java.io.FileOutputStream;
import java.io.BufferedOutputStream;
public class Main {
public static void main(String[] args) {
String data = "This is a line of text inside the file";
try {
// Creates a FileOutputStream
FileOutputStream file = new FileOutputStream("output.txt");
// Creates a BufferedOutputStream
BufferedOutputStream output = new BufferedOutputStream(file);
byte[] array = data.getBytes();
// Writes data to the output stream
output.write(array);
output.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}