Chapter 8 IO
THE OCP EXAM TOPICS COVERED IN THIS CHAPTER INCLUDE THE FOLLOWING:
- ✓ Java I/O Fundamentals
- Read and write data from the console
- Use BufferedReader, BufferedWriter, File, FileReader, FileWriter, FileInputStream, FileOutputStream, ObjectOutputStream, ObjectInputStream, and PrintWriter in the java.io.package.
What can Java applications do outside the scope of managing objects and attributes in memory? How can they save data so that information is not lost every time the program is terminated? They use files, of course! You can design code that writes the current state of an application to a file every time the application is closed and then reloads the data when the application is executed the next time. In this manner, information is preserved between program executions.
This chapter focuses on using the java.io
API to interact with files and streams. We start by describing how files and directories are organized within a file system and show how to access them with the java.io.File
class. We then show how to read and write file data with the stream classes. Finally, we conclude this chapter by discussing ways of reading user input at runtime using the Console
class.
In the next chapter, “NIO.2,” we will revisit the discussion of files and show how Java now provides more powerful techniques for managing files.
Understanding Files and Directories
We begin this ...
Get OCP Oracle® Certified Professional Java® SE 8 Programmer II 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.