Java Tutorial
Operators
Flow Control
String
Number and Date
Built-in Classes
Array
Class and Object
Inheritance and Polymorphism
Exception Handling
Collections, Generics and Enumerations
Reflection
Input/Output Stream
Annotation
Byte streams in Java are used to perform input and output operations on binary data (bytes) such as images, audio, or other binary file formats. The java.io
package provides classes to work with byte streams.
In this tutorial, we will go through some of the commonly used byte stream classes and their usage.
FileInputStream
and FileOutputStream
are used for reading and writing binary data to and from files, respectively.
Here is an example demonstrating how to read a binary file (e.g., an image) and copy its content to another file using FileInputStream
and FileOutputStream
:
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class ByteStreamExample { public static void main(String[] args) { try { FileInputStream inputStream = new FileInputStream("input.jpg"); FileOutputStream outputStream = new FileOutputStream("output.jpg"); int data; while ((data = inputStream.read()) != -1) { outputStream.write(data); } inputStream.close(); outputStream.close(); System.out.println("File copied successfully!"); } catch (IOException e) { e.printStackTrace(); } } }
In this example, we read the binary data from input.jpg
and write it to output.jpg
. read()
method returns the byte value read or -1
if the end of the file is reached.
BufferedInputStream
and BufferedOutputStream
are wrapper classes that provide buffering for input and output byte streams, respectively. These classes improve the efficiency of file operations by reducing the number of direct read/write operations on the underlying stream.
Here's the previous example modified to use BufferedInputStream
and BufferedOutputStream
:
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class BufferedByteStreamExample { public static void main(String[] args) { try { FileInputStream fileInputStream = new FileInputStream("input.jpg"); FileOutputStream fileOutputStream = new FileOutputStream("output.jpg"); BufferedInputStream inputStream = new BufferedInputStream(fileInputStream); BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream); int data; while ((data = inputStream.read()) != -1) { outputStream.write(data); } inputStream.close(); outputStream.close(); System.out.println("File copied successfully!"); } catch (IOException e) { e.printStackTrace(); } } }
In this example, BufferedInputStream
and BufferedOutputStream
are used to improve the efficiency of reading and writing binary data to and from files.
These are the basics of working with byte streams in Java. Using the java.io
package, you can perform various input and output operations on binary data efficiently. Note that when working with character data, it is recommended to use character streams (e.g., FileReader
and FileWriter
) instead of byte streams.
Java FileInputStream
and FileOutputStream
:
FileInputStream
and FileOutputStream
are used for reading and writing raw binary data from and to files.
FileInputStream fis = new FileInputStream("input.txt"); FileOutputStream fos = new FileOutputStream("output.txt");
Reading and Writing Bytes in Java:
Use FileInputStream
to read bytes from a file and FileOutputStream
to write bytes to a file.
int byteData; while ((byteData = fis.read()) != -1) { fos.write(byteData); }
BufferedInputStream
and BufferedOutputStream
in Java:
These classes provide buffered reading and writing for improved performance.
BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos);
DataInputStream
and DataOutputStream
in Java:
These classes provide methods for reading and writing primitive data types.
DataInputStream dis = new DataInputStream(bis); DataOutputStream dos = new DataOutputStream(bos);
ObjectInputStream
and ObjectOutputStream
in Java:
Used for reading and writing Java objects.
ObjectInputStream ois = new ObjectInputStream(fis); ObjectOutputStream oos = new ObjectOutputStream(fos);
Reading Binary Files with Java Byte Streams: Use byte streams to read binary files.
FileInputStream fis = new FileInputStream("binaryfile.dat");
Java Byte Streams vs Character Streams: Byte streams are suitable for binary data, while character streams are designed for character data.
FileInputStream fis = new FileInputStream("binaryfile.dat"); FileReader reader = new FileReader("textfile.txt");
Handling Exceptions with Java Byte Streams: Handle exceptions to ensure proper error handling.
try { FileInputStream fis = new FileInputStream("input.txt"); // Reading logic } catch (IOException e) { e.printStackTrace(); } finally { // Close resources }
Java Byte Streams for File Input and Output: Use byte streams for reading and writing files.
FileInputStream fis = new FileInputStream("input.txt"); FileOutputStream fos = new FileOutputStream("output.txt");
Using ByteArrayInputStream
and ByteArrayOutputStream
in Java:
These classes allow reading and writing bytes to an in-memory byte array.
byte[] byteArray = { /* some bytes */ }; ByteArrayInputStream bais = new ByteArrayInputStream(byteArray); ByteArrayOutputStream baos = new ByteArrayOutputStream();
Copying Files with Java Byte Streams: Use byte streams to copy data from one file to another.
FileInputStream fis = new FileInputStream("source.txt"); FileOutputStream fos = new FileOutputStream("destination.txt"); int data; while ((data = fis.read()) != -1) { fos.write(data); }
FilterInputStream
and FilterOutputStream
in Java:
Filter streams provide additional functionality.
FilterInputStream filterInputStream = new DataInputStream(new FileInputStream("datafile.dat"));
PipedInputStream
and PipedOutputStream
in Java:
These streams are used for communication between threads.
PipedInputStream pipedInputStream = new PipedInputStream(); PipedOutputStream pipedOutputStream = new PipedOutputStream(); pipedInputStream.connect(pipedOutputStream);
SequenceInputStream
in Java:
Concatenates multiple input streams.
FileInputStream fis1 = new FileInputStream("file1.txt"); FileInputStream fis2 = new FileInputStream("file2.txt"); SequenceInputStream sequenceInputStream = new SequenceInputStream(fis1, fis2);
Java Byte Streams and Network Programming: Byte streams are commonly used in network programming.
Socket socket = new Socket("localhost", 8080); InputStream inputStream = socket.getInputStream();
Java Byte Streams for Reading and Writing Arrays: Read and write byte arrays using byte streams.
byte[] data = { /* some bytes */ }; ByteArrayInputStream bais = new ByteArrayInputStream(data);
Concurrency and Java Byte Streams: Handle concurrency issues when working with byte streams in multithreaded environments.
// Consider synchronization mechanisms when using streams concurrently