Java I/O Streams overview

Importing Data in Java

Anthony Markham

VP Quant Developer

Stream fundamentals

  • Streams are unidirectional pipes of data with FIFO (First In, First Out) order
  • Byte streams work with raw binary data (InputStream/OutputStream)
  • Character streams handle text with automatic encoding (Reader/Writer)

Streams representation

Importing Data in Java

Byte Streams

  • Used for binary data (images, audio, etc.)
  • Base abstract classes: InputStream (reading), OutputStream (writing)
  • Key implementations: FileInputStream, FileOutputStream
  • Key methods: read() returns bytes, write() outputs bytes

Binary files: images and audios

Importing Data in Java

Byte Streams

try (FileInputStream fis = new FileInputStream("data.bin")) {

byte[] buffer = new byte[1024]; int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) { // Process bytes in buffer
System.out.println("Read " + bytesRead + " bytes"); }
} catch (Exception e) { System.err.println("Error: " + e.getMessage()); }
Importing Data in Java

Character Streams

  • Designed specifically for text data
  • Base abstract classes: Reader (reading text), Writer (writing text)
  • Key implementations: FileReader, FileWriter, BufferedReader, BufferedWriter
  • Convenient methods: readLine() reads a full line of text

CSV files

Importing Data in Java

Character Streams

try (BufferedReader reader = new BufferedReader(new FileReader("data.csv"))) {
    String line;

while ((line = reader.readLine()) != null) { // Process each line System.out.println(line); }
} catch (Exception e) { System.err.println("Error: " + e.getMessage()); }
Importing Data in Java

Buffered Streams - performance

  • Improves performance by reducing disk operations
  • Read/write byte streams with BufferedInputStream and BufferedOutputStream
  • Read/write character streams with BufferedReader, BufferedWriter
    • Read an entire line with readLine()
Importing Data in Java

Buffered Streams - performance

try (BufferedReader reader = new BufferedReader(new FileReader("data.csv"))) {
    // Header processing
    String header = reader.readLine();
    System.out.println("Header: " + header);

// Efficient data reading String line; int count = 0; while ((line = reader.readLine()) != null) { count++; } System.out.println("Read " + count + " data rows"); } catch (Exception e) { System.out.println("Error: " + e.getMessage()); }
Importing Data in Java

Stream chaining

  • Each stream in the chain adds specific functionality
  • Common pattern: FileInputStream -> InputStreamReader -> BufferedReader
  • Always close the outermost stream (others close automatically)
Importing Data in Java

Stream chaining

import java.nio.charset.StandardCharsets;
try (

BufferedReader reader = new BufferedReader(
new InputStreamReader(
new FileInputStream("data.csv"),
StandardCharsets.UTF_8 )
)
) { String line; while ((line = reader.readLine()) != null) { System.out.println(line);} } catch (Exception e) { System.err.println("Error: " + e.getMessage()); }
Importing Data in Java

Exception handling

  • Crucial for I/O operations
  • Common exceptions: IOException, FileNotFoundException
  • Always use try-with-resources to catch exceptions
Importing Data in Java

Exception handling

import java.util.ArrayList;
import java.util.List;
public static List<String> readLines(String filePath) throws IOException {

List<String> lines = new ArrayList<>(); try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) { String line; while ((line = reader.readLine()) != null) { lines.add(line); } } // Stream automatically closed here return lines;
}
Importing Data in Java

Let's practice!

Importing Data in Java

Preparing Video For Download...