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
Eric McIntyre
Eric McIntyre14mo ago
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){}
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
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);
}
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.
Eric McIntyre
Eric McIntyre14mo ago
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(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);
}
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
Eric McIntyre
Eric McIntyre14mo ago
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_
Eric McIntyre
Eric McIntyre14mo ago
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(); } } }
⭐ Submission from Deepakkumard#5137
Want results from more Discord servers?
Add your server