没有合适的资源?快使用搜索试试~ 我知道了~
Pro Java 7 NIO.2
5星 · 超过95%的资源 需积分: 10 7 下载量 65 浏览量
2014-06-17
13:13:28
上传
评论
收藏 4.15MB PDF 举报
温馨提示
试读
296页
This book covers all the important aspects involved in developing NIO.2-based applications. It provides clear instructions for getting the most out of NIO.2 and offers many exercises and case studies to spice up your Java 7 applications with the new I/O capabilities. You will learn to develop NIO.2 applications, beginning with simple but essential stuff and gradually moving on to complex features such as sockets and asynchronous channels.
资源推荐
资源详情
资源评论
Leonard
Shelve in
Programming Languages/Java
User level:
Intermediate–Advanced
www.apress.com
SOURCE CODE ONLINE
RELATED
BOOKS FOR PROFESSIONALS BY PROFESSIONALS
®
Pro Java 7 NIO.2
Pro Java 7 NIO.2 gives you the skills to write robust, scalable Java applications using
NIO.2. It covers the three primary elements that offer new input/output (I/O) APIs in
Java 7, showing you how to:
• Use the extensive file I/O API system that developers have long sought
• Work with the socket channel API to carry out multicasting and socket binding
associated with channels
• Enhance scalability with the asynchronous I/O API: map to I/O facilities, completion
ports, and various I/O event port mechanisms
With Pro Java 7 NIO.2, you’ll learn how to:
• Get/set file metadata through the java.nio.file.attribute API (including POSIX)
• Manage symbolic and hard links
• Deal with files and directories through the new java.nio.file.Files API
• Use the FileVisitor API to develop recursive file operations
• Explore the Watch Service API and file change notification
• Use the new SeekableByteChannel API for working with random access files
• Develop blocking/non-blocking socket-based applications
• Use the jewel in the crown of NIO.2: the Asynchronous Channel API
• Refactor java.io.File code
Take your Java applications to the next level with Pro Java 7 NIO.2. Each chapter con-
tains extensive code examples that show the power and elegance of NIO.2, giving you
the knowledge to apply the latest and greatest techniques in your own code.
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
iii
Contents at a Glance
Contents at a Glance ....................................................................................................... iii
Contents .......................................................................................................................... iv
About the Author ........................................................................................................... xiii
About the Technical Reviewer ....................................................................................... xiv
Acknowledgments .......................................................................................................... xv
Preface .......................................................................................................................... xvi
Chapter 1: Working with the Path Class ........................................................................... 1
Chapter 2: Metadata File Attributes ............................................................................... 11
Chapter 3: Manage Symbolic and Hard Links ................................................................. 35
Chapter 4: Files and Directories ..................................................................................... 43
Chapter 5: Recursive Operations: Walks ........................................................................ 77
Chapter 6: Watch Service API ....................................................................................... 111
Chapter 7: Random Access Files .................................................................................. 135
Chapter 8: The Sockets APIs ........................................................................................ 169
Chapter 9: The Asynchronous Channel API .................................................................. 215
Chapter 10: Important Things to Remember ................................................................ 263
Index ............................................................................................................................ 273
C H A P T E R 1
■ ■ ■
1
Working with the Path Class
The recommended entry point to start exploring the NIO.2 API, also known as “JSR 203: More New I/O
APIs for the Java Platform” (NIO.2), is the new abstract class java.nio.file.Path. This class is a
milestone of NIO.2, and every application that involves I/O operations will exploit the powerful facilities
of this class. Practically, it is the most commonly used class of NIO.2, since many I/O operations are
based on a Path resource.
The Path class supports two types of operations: syntactic operations (almost any operation that
involves manipulating paths without accessing the file system; these are logical manipulations done in
memory) and operations over files referenced by paths. This chapter covers the first type of operations
and introduces you to the Path API. In Chapter 4, I focus on exploring the second type of operations. The
concepts presented in this chapter will be very useful in the rest of the book.
Introducing the Path Class
A path resides in a file system, which “stores and organizes files on some form of media, generally one or
more hard drives, in such a way that they can be easily retrieved.”
1
The file system can be accessed
through the java.nio.file.FileSystems final class, which is used to get an instance of the
java.nio.file.FileSystem we want to work on. FileSystems contains the following two important
methods, as well as a set of newFileSystem() methods, for constructing new file systems:
• getDefault(): This is a static method that returns the default FileSystem to the
JVM—commonly the operating system default file system.
1
Oracle, The Java Tutorials, “What Is a Path? (And Other File System Facts),”
http://download.oracle.com/javase/tutorial/essential/io/path.html.
CHAPTER 1 ■ WORKING WITH THE PATH CLASS
2
• getFileSystem(URI uri): This is a static method that returns a file system from
the set of available file system providers that match the given URI schema. The
Path class manipulates a file in any file system (FileSystem) that can use any
storage place (java.nio.file.FileStore; this class represents the underlying
storage). By default (and commonly), the Path refers to files in the default file
system (the file system of the computer), but NIO.2 is totally modular—an
implementation of FileSystem for data in memory, on the network, or on a virtual
file system is perfectly agreeable to NIO.2. NIO.2 provides us with all file system
functionalities that we may need to perform over a file, a directory, or a link.
The Path class is an upgraded version of the well-known java.io.File class, but the File class has
kept a few specific operations, so it is not deprecated and cannot be considered obsolete. Moreover,
starting with Java 7, both classes are available, which means programmers can mix their powers to
obtain the best of I/O APIs. Java 7 provides a simple API for conversion between them. Remember the
days when you had to do the following?
import java.io.File;
…
File file = new File("index.html");
Well, those days are gone, because with Java 7 you can do this:
import java.nio.file.Path;
import java.nio.file.Paths;
…
Path path = Paths.get("index.html");
At a closer look, a Path is a programmatic representation of a path in the file system. The path string
contains the file name, the directory list, and the OS-dependent file delimiter (e.g., backslash “\” on
Microsoft Windows and forward slash “/” on Solaris and Linux), which means that a Path is not system
independent since it is based on a system-dependent string path. Because Path is basically a string, the
referenced resource might not exist.
Defining a Path
Once you identify the file system and the location of a file or directory, you can create a Path object for it.
Absolute paths, relative paths, paths defined with the notation “.” (indicates the current directory) or
“..” (indicates the parent directory), and paths containing only a file/directory name are covered by the
Path class. The simplest solution for defining a Path is to call one of the get() methods of the Paths
helper class. The following subsections present several different ways to define a path to the same file
(on Windows)—C:\rafaelnadal\tournaments\2009\BNP.txt.
Define an Absolute Path
An absolute path (also known as a full path or file path) is a path that contains the root directory and all
other subdirectories that contain a file or folder. Defining an absolute path in NIO.2 is a one-line-of-code
task, as you can see in the following example, which points to the file named BNP.txt in the
C:\rafaelnadal\tournaments\2009 directory (the file may not exist for testing this code):
Path path = Paths.get("C:/rafaelnadal/tournaments/2009/BNP.txt");
剩余295页未读,继续阅读
资源评论
- Jerry__SK2014-08-06出版社的pdf,但是顺序有错乱,目录放到了最后
baidu_16615059
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 农村信用社联合社计算机信息系统投产与变更管理办.docx
- 农村信用社联合社计算机信息系统数据管理办法.docx
- 利用SPSS作临床效度分析线上计算网站介绍-医学研究部统计谘.(医学PPT课件).ppt
- 利用Zabbix监控mysqldump定时备份数据库状态.docx
- 利用计算机解决问题的基本过程.doc
- 化工铁路通信工程总结.doc
- 北京大学网络教育软件工程作业.docx
- 医药公司(连锁店)计算机操作规程未新系统的自行按照旧制修改-新系统过制的编号加修模版.doc
- 医药公司(连锁店)计算机系统操作规程模版.doc
- 医药连锁门店计算机系统的操作和管理程序未新系统的自行按照旧制修改-新系统过制的编号加修模版.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功