Chapter 2

Working with File Streams

In This Chapter

arrow Knowing your Java streams

arrow Reading and writing text streams

arrow 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 System.in, 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, 4th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.