10.2 Text-File I/O
Proper words in proper places,
make the true definition of a style.
—JONATHAN SWIFT, Letter to a Young Clergyman (January 9,1720)
In this section, we give a description of the most common ways to perform text-file I/O in Java.
Creating a Text File
The class PrintWriter
in the Java Class Library defines the methods that we will need to create and write to a text file. This class is the preferred one for writing to a text file. It is in the package java.io
, so we will need to begin our program with an import
statement. Actually, we will be using other classes as well, so we will import them also, as you will see soon.
Before we can write to a text file, we must connect it to an output stream. That is, we open the file. To do ...
Get Java: An Introduction to Problem Solving and Programming, 8th 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.