#Week 45 — What's the purpose of the `InputStream` and `OutputStream` classes?

5 messages · Page 1 of 1 (latest)

echo sageBOT
#
Question of the Week #45

What's the purpose of the InputStream and OutputStream classes?

spark totemBOT
#

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/OutputStreams in order to write/read various kinds of data like Java primitives and Strings.

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:

public record SomeData(byte b, int i, String s){}

It is then possible to save objects of that record to a file:

SomeData data = new SomeData((byte)69, 420, "Hello World");
try(DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("someFile.bin")))){
    dos.writeByte(data.b());
    dos.writeInt(data.i());
    dos.writeUTF(data.s());
}//all streams are closed with the try-with-resources block

In order to load that again, something similar to this can be used:

try(DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("someFile.bin")))){
    byte b = dis.readByte();
    int i = dis.readInt();
    String s = dis.readUTF();
    SomeData data = new SomeData(b, i, s);
    System.out.println(data);
}

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:

try(Writer writer =new BufferedWriter(new OutputStreamWriter(new FileOutputStream("someFile.txt"), StandardCharsets.UTF_8))){
    writer.write("Hello World");
}
try(BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream("someFile.txt"), StandardCharsets.UTF_8))){
    String firstLine = br.readLine();
    System.out.println(firstLine);
}
📖 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_