Java IO Streams Overview
Various Stream Types
Memory Streams
ByteArrayInputStream - Memory input stream
ByteArrayOutputStream - Memory output stream
Important Notes:
- Memory streams interact between the program and memory, not with files
- Memory streams are channels from the program to memory and cannot be closed
Use Cases: Frequently used data in projects can be backed up using memory streams.
Why Use Memory Streams: Memory (server) and disk (database) have frequent interactions with low efficiency. Using Redis to cache data base data reduces these interactions. The cache database utilizes memory streams.
Memory Output Stream
Understanding: Constructor with and without parameters, array creation, writing arrays using the write method in a loop, and its expansion mechanism. It doesn't return the array address directly but copies it for security and thread safety, returning the data as a string.
public class MemoryOutputStreamExample {
public static void main(String[] args) throws IOException {
// 1. Create stream object
ByteArrayOutputStream memOut = new ByteArrayOutputStream();
// Memory streams cannot be closed as they're channels from program to memory
// memOut.close();
// 2. Write data - writes data to the byte array in memOut object
memOut.write("123abcMemoryTest".getBytes());
// Get data from the stream object
System.out.println(new String(memOut.toByteArray()));
System.out.println(memOut.toString());
}
}
Memory Input Stream
public class MemoryInputStreamExample {
public static void main(String[] args) throws IOException {
// 1. Create stream object
ByteArrayInputStream memIn = new ByteArrayInputStream("123abcMemoryTest".getBytes());
// Memory streams cannot be closed as they're channels from program to memory
// memIn.close();
// 2. Read data
byte[] buffer = new byte[1024];
int length;
while((length = memIn.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, length));
}
}
}
Print Streams
PrintStream - Byte print stream
PrintWriter - Character print stream
Note: Print streams are essentially output streams with only one direction (program → file).
PrintStream vs PrintWriter
- Difference 1: PrintStream operates at the byte level, while PrintWriter operates at the character level
- Difference 2:
- PrintStream: Converts byte streams to byte print streams
- PrintWriter: Converts both byte and character streams to character print streams
Byte Print Stream
public class BytePrintStreamExample {
public static void main(String[] args) throws IOException {
// Create a byte print stream to standard output
PrintStream bytePrinter = new PrintStream(System.out);
// Write different data types
bytePrinter.print("This is a string ");
bytePrinter.println(123);
bytePrinter.println(45.67);
bytePrinter.println(true);
// Custom output stream
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
PrintStream customBytePrinter = new PrintStream(byteOut);
customBytePrinter.println("Data written to custom stream");
// Display the data
System.out.println("Data in custom stream: " + byteOut.toString());
}
}
Character Print Stream
public class CharacterPrintStreamExample {
public static void main(String[] args) throws IOException {
// Create a character print stream to standard output
PrintWriter charWriter = new PrintWriter(System.out);
// Write different data types
charWriter.print("This is a string ");
charWriter.println(123);
charWriter.println(45.67);
charWriter.println(true);
// Flush the writer to ensure all data is written
charWriter.flush();
// Custom output stream
StringWriter stringWriter = new StringWriter();
PrintWriter customWriter = new PrintWriter(stringWriter);
customWriter.println("Data written to custom writer");
// Display the data
System.out.println("Data in custom writer: " + stringWriter.toString());
}
}
Introduction to Network Programming
Network programming in Java involves communication between applications over a network. Java provides extensive APIs for network operations, primarily through the java.net package.
Key concepts in network programming include:
- IP Address: Unique identifier for devices on a network
- Port: Communication endpoint for specific services
- Protocol: Rules for data communication (TCP, UDP, etc.)
- Sockets: Programming interface for network communication
Java supports both TCP (reliable, connection-oriented) and UDP (unreliable, connectionless) protocols for network communication.