Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 9

Java File I/O (NIO.2)

Use the Path class to operate on file and directory paths

Use the Files class to check, delete, copy, or move a file or directory

Read and change file and directory attributes

Exam Topics

Recursively access a directory tree

Find a file by using the PathMatcher class

Watch a directory for changes by using WatchService

We covered I/O fundamentals in the last chapter, where you learned how to read and write from console and how to use streams to read and write to files. In this chapter, you will learn how to work with file systems—for example, how to save a file/directory; create a file/directory; navigate directories; copy, move, or delete a file/directory; and so on. As a Java programmer, you should be aware how to programmatically achieve these file/directory operations.

Java offers a rich set of APIs to manipulate files and directories. In fact, Java 7 introduces a new set of I/O APIs called NIO.2 that offer convenient ways to perform operations related to a file system. In this chapter, you will explore how to perform various file operations such as create, move, copy, and delete. You will also learn how to search files in a directory structure and how to get/set properties of files/directories.

A Quick History of I/O APIs

Initially, Java offered the File class (in the java.io package) to access file systems. This class represents a file/ directory in the file system and allows you to perform operations such as checking the existence of a file/directory, getting the properties, and deleting a file/directory. However, the first version of the API was not sufficient to meet the needs of developers, and a need for improved I/O APIs was felt. In brief, the following shortcomings were noticed in the first version of the Java I/O APIs:

The File class lacked the significant functionality required to implement even commonly used functionality. For instance, it lacked a copy method to copy a file/directory.

251

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]