The byte stream or binary classes in Java are used to read and write data in the form of bytes to files like images, videos, etc. The byte stream abstract base classes are InputStream and OutputStream. The input byte stream class hierarchy is shown in the below figure.
The description of these input byte stream classes is provided in the below table:
Stream class | Description |
InputStream | This is an abstract base class for all input byte stream classes. |
FileInputStream | An input byte stream class used to read from files. |
ByteArrayInputStream | An input byte stream class for reading data from a byte array as an input stream. |
ObjectInputStream | An input byte stream class for reading data as objects from a file |
PipedInputStream | An input byte stream class for reading data from a pipe which is used for inter-thread communication. |
FilterInputStream | An input byte stream class used to filter the data from the underlying data stream. |
DataInputStream | An input byte stream class than can read data as Java standard types. |
BufferedInputStream | An input byte stream class which is faster than other input stream classes. |
PushBackInputStream | An input byte stream class which can be used to look ahead in the input stream. |
Following program reads characters from a file using the byte stream classes: InputStream and FileInputStream:
import java.io.*;
class InputStreamDemo
{
public static void main(String [] args) throws Exception
{
InputStream is = new FileInputStream("data.txt");
int ch;
while((ch = is.read()) != -1)
{
System.out.print((char)ch);
}
is.close();
}
}
Output for the above program is:
Startertutorials
The output byte stream class hierarchy is shown in the below figure.
The description of these output byte stream classes is provided in the below table:
Stream class | Description |
OutputStream | This is an abstract base class for all output byte stream classes. |
FileOutputStream | An output byte stream class used to write data to files. |
ByteArrayOutputStream | An output byte stream class for writing data to a byte array as an output stream. |
ObjectOutputStream | An output byte stream class for writing data as objects to a file |
PipedOutputStream | An output byte stream class for writing data to a pipe which is used for inter-thread communication. |
FilterOutputStream | An output byte stream class used to filter the data from the underlying data stream. |
DataOutputStream | An output byte stream class than can write data as Java standard types. |
BufferedOutputStream | An output byte stream class which is faster than other output stream classes. |
PushBackOutputStream | An output byte stream class which can be used to look ahead in the output stream. |
Following program writes characters to a file using the byte stream classes: OutputStream and FileOutputStream:
import java.io.*;
class OutputStreamDemo
{
public static void main(String [] args) throws Exception
{
OutputStream os = new FileOutputStream("data.txt");
byte data[] = {115,116,117,116,115};
os.write(data);
os.flush();
os.close();
}
}
Output for the above program is:
Stuts
For more information, visit the following links:
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
Leave a Reply