ByteStream Classes in Java
In this tutorial, we will learn about ByteStream classes in Java. Java provides ByteStream classes to read bytes from the input stream and write bytes to the output stream. By using this class we can read and write data into files.
ByteStream classes are the part of java.io
package.
From the picture given below we can get the idea of work of ByteStream classes. As given in the picture the classes work as mediater while reading and writing to the file.
Mainly the ByteStream classes are divided into two types of classes, i.e., InputStream and OutputStream. These classes are abstract and the superclasses of all the Input/Output stream classes.
InputStream Class
This class presents methods to read bytes from a file, console, or memory. It is an abstract class and can't be instantiated; however, various classes inherit the InputStream class and override its methods.
Subclasses of InputStream class are given in the following table:
Class Name
|
Description
|
BufferedInputStream
|
This class provides methods to read bytes from the buffer.
|
ByteArrayInputStream
|
This class provides methods to read bytes from the byte array.
|
DataInputStream
|
This class provides methods to read Java primitive data types.
|
FileInputStream
|
This class provides methods to read bytes from a file.
|
FilterInputStream
|
This class contains methods to read bytes from the other input streams, which are used as the primary source of data.
|
ObjectInputStream
|
This class provides methods to read objects.
|
PipedInputStream
|
This class provides methods to read from a piped output stream to which the piped input stream must be connected.
|
SequenceInputStream
|
This class provides methods to connect multiple Input Stream and read data from them.
|
The InputStream class includes various methods to read the data from an input stream. These methods are overridden by the classes that inherit the InputStream class.
Methods of subclasses of InputStream class are given below:
Method Name
|
Description
|
int read()
|
This method returns an integer, an integral representation of the next available byte of the input. The integer -1 is returned once the end of the input is encountered.
|
int read (byte buffer [])
|
This method is used to read the specified buffer length bytes from the input and returns the total number of bytes successfully read. It returns -1 once the end of the input is encountered.
|
int read (byte buffer [], int loc, int nBytes)
|
This method is used to read the 'nBytes' bytes from the buffer starting at a specified location, 'loc'. It returns the total number of bytes successfully read from the input. It returns -1 once the end of the input is encountered.
|
int available ()
|
This method returns the number of bytes that are available to read.
|
void mark(int nBytes)
|
This method is used to mark the current position in the input stream until the specified nBytes are read.
|
void reset ()
|
This method is used to reset the input pointer to the previously set mark.
|
long skip (long nBytes)
|
This method is used to skip the nBytes of the input stream and returns the total number of bytes that are skipped.
|
void close ()
|
This method is used to close the input source. If an attempt is made to read even after the closing, IOException is thrown by the method.
|
OutputStream Class
The OutputStream is an abstract class that is used to write 8-bit bytes to the stream. It is the superclass of all the output stream classes.
Note: OutputStream class cannot be instantiated, which means we cannot create an object of this class. On the other hand, it is inherited by following classes.
This class is inherited by various subclasses that are given in the following table.
Class Name
|
Description
|
BufferedOutputStream
|
This class provides methods to write the bytes to the buffer. |
ByteArrayOutputStream
|
This class provides methods to write bytes to the byte array.
|
DataOutputStream
|
This class provides methods to write the java primitive data types.
|
FileOutputStream
|
This class provides methods to write bytes to a file.
|
FilterOutputStream
|
This class provides methods to write to other output streams.
|
ObjectOutputStream
|
This class provides methods to write objects.
|
PipedOutputStream
|
It provides methods to write bytes to a piped output stream.
|
PrintStream
|
It provides methods to print Java primitive data types.
|
The OutputStream class provides various methods to write bytes to the output streams. The methods are given in the following table:
Method
|
Description
|
void write (int i)
|
This method is used to write the specified single byte to the output stream.
|
void write (byte buffer [] )
|
It is used to write a byte array to the output stream.
|
void write(bytes buffer[],int loc, int nBytes)
|
It is used to write nByte bytes to the output stream from the buffer starting at the specified location.
|
void flush ()
|
It is used to flush the output stream and writes the pending buffered bytes.
|
void close ()
|
It is used to close the output stream. However, if we try to close the already closed output stream, the IOException will be thrown by this method.
|
Example for basic writing and reading text from a file
In the following example, we have a string "Welcome to Studytonight" firstly we convert them to an array of bytes using the getBytes()
method. Then using read()
method of ByteArrayInputStream
class we read data from an array and storing it to the myfile.txt
file using the write()
method of FileOutputStream
class.
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class StudyTonight
{
public static void main(String[] args) throws IOException
{
byte data[] = "Welcome to Studytonight".getBytes();
ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
inputStream.read(data);
File newFile = new File("E:\\studytonight\\myfile.txt");
FileOutputStream outputStream = new FileOutputStream(newFile);
outputStream.write(data);
}
}
New file myfile.txt
will be created in a specified path and there following output will be present.
Welcome to Studytonight
Conclusion
In this tutorial, we learned about the ByteStream classes i.e. InputStream and OutputStream, these classes have various methods for different purposes. ByteStream class is used to read and write data in the file.