Chapter 2

Working with File Streams


check Knowing your Java streams

check Reading and writing text streams

check Reading and writing binary streams

I/O, I/O, it’s off to work I go.

Or so goes the classic song, which pretty much sums up the whole purpose of computers. Without I/O (input/output), computers — and the programs that run on them — would be worthless.

Imagining any useful computer program that doesn’t do some form of I/O is hard. Even the very first program presented in this book — the classic Hello, World! program — does I/O; it displays a simple message onscreen.

In this chapter, you find out about Java’s most fundamental technique for getting data into and out of programs: streams. You’ve been working with streams all along in this book. When you use the System.out.print or System.out.println method to display text on the console, you’re actually sending data to an output stream, and when you use a Scanner object to get data from, you’re reading data from an input stream.

In this chapter, you build on what you already know about stream I/O and see how it can be used to read and write data to hard-drive files.

Understanding Streams

A stream is simply a flow ...

Get Java All-in-One For Dummies, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.