Week 45 — What's the purpose of the `InputStream` and `OutputStream` classes?
Question of the Week #45
What's the purpose of the
InputStream
and OutputStream
classes?4 Replies
The JDK provides the
abstract
classes InputStream
and OutputStream
as a common way to access binary input/output.
InputStream
is used for reading (binary) data while OutputStream
allows writing (binary) data.
The JDK defines multiple subclasses of InputStream
/OutputStream
for different data sources or different kinds of processing.
For example, FileInputStream
/FileOutputStream
can be used for accessing reading/writing data from/to files while ByteArrayInputStream
and ByteArrayOutputStream
are useful for reading from a byte[]
or creating a byte[]
using an InputStream
/OutputStream
.
Other streams like BufferedInputStream
/BufferedOutputStream
can be added to another InputStream
/OutputStream
in order to buffer incoming/outgoing data so it doesn't need to be sent/received byte per byte.
Furthermore, DataInputStream
/DataOutputStream
can be combined with other InputStream
/OutputStream
s in order to write/read various kinds of data like Java primitives and String
s.
These are just a few out of many InputStream
/OutputStream
classes provided by Java. It is also possible to create custom subclasses.
Assume we want to save and load an object containing a byte
, an int
and a String
. That object can be modelled using a record:
It is then possible to save objects of that record to a file:
In order to load that again, something similar to this can be used:
When reading/writing data with streams like DataInputStream
/DataOutputStream
, it is important that data is read in the same order it has been written in.For editing text, Java provides the
Reader
/Writer
classes and subclasses of them.
For example, it is possible to create a FileReader
/FileWriter
for accessing files or use InputStreamReader
/OutoutStreamWriter
to read/write text content from/to an InputStream
/OutputStream
:
📖 Sample answer from dan1st
InputStream: Just to recieve data from an open flow, for example you can open a flow with inputstream to recieve data from an other process.
OutputStream: It's the oppsite from inputStream, u send data from the process where u open de flow (ouputStream) to another process for example
Submission from rubendd_
InputStream and OutputStream are abstract classes, they provide a foundation for reading from and writing to various data sources and destinations. These classes are used for handling binary data and are particularly important for dealing with file I/O, network communication, and other input and output operations.
import java.io.*;
public class FileReadWrite{
public static void main(String[] args) {
try (InputStream in = new FileInputStream("source.txt");
OutputStream out = new FileOutputStream("destination.txt")) {
byte[] buffer = new byte[1024]; int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } catch (IOException e) { e.printStackTrace(); } } }
byte[] buffer = new byte[1024]; int bytesRead;
while ((bytesRead = in.read(buffer)) != -1) { out.write(buffer, 0, bytesRead); } } catch (IOException e) { e.printStackTrace(); } } }
⭐ Submission from Deepakkumard#5137