Working with File Streams
IN THIS CHAPTER
Knowing your Java streams
Reading and writing text streams
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.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.
A stream is simply a flow ...