File System API 428204
-
Upload
spidermanpc -
Category
Documents
-
view
222 -
download
0
Transcript of File System API 428204
-
8/6/2019 File System API 428204
1/21
1 | 2011 Oracle Corporation
-
8/6/2019 File System API 428204
2/21
2 | 2011 Oracle Corporation
The New File System API in JDK 7
Staffan Friberg
-
8/6/2019 File System API 428204
3/21
3 | 2011 Oracle Corporation
Agenda
Background
File System API Common classes
API Features
Service Provider Interface
Summary
-
8/6/2019 File System API 428204
4/21
4 | 2011 Oracle Corporation
Background and Motivation
Need something better than java.io.File Doesn't work consistently across platforms
No useful exceptions when a file operation fails
Missing basic operations (file copy, move, ...)
Limited support for symbolic links
Limited support for file attributes, performance issues
No way to plug-in other file system implementations
Need a new file system API
JSR-203 tasked to define this and more
-
8/6/2019 File System API 428204
5/21
5 | 2011 Oracle Corporation
Main concepts
Path Locates a file in a file system
Files Static methods to operate on files and directories
FileSystem
A handle to a file system and factory for objects that access FileSystems.getDefault() returns the local/platform file syste
FileStore The underlying storage (volume, concrete file system ...)
-
8/6/2019 File System API 428204
6/21
6 | 2011 Oracle Corporation
Path
Immutable
Create from path String, URI or java.io.File
Syntax is essentially [name]+ or root [name]* foo, /, /foo, foo/bar C:\, C:\foo, \\foofighter\bar
Defines methods to access, compare and modify P
Support old libraries
Create File from Path using toFile
http://foofighter/bar -
8/6/2019 File System API 428204
7/21
7 | 2011 Oracle Corporation
Files
Static methods to operate on files and directories
Create/open for reading and write
Copy, move and delete
Read and set file attributes
Methods accessing the filesystem may throw IOExc Specific exceptions for some recoverable errors
-
8/6/2019 File System API 428204
8/21
8 | 2011 Oracle Corporation
File Attributes
Groups of related attributes
Define view that provides
Typesafe access to attributes in group
Bulk access where appropriate
Convert to/from the file system representation
Implementations required to support Basic view Common file systems attributes (size, type, timestamp
Implementations may support additional views
-
8/6/2019 File System API 428204
9/21
9 | 2011 Oracle Corporation
File Attributes
PosixFileAttributes attrs =
Files.readAttributes(path, PosixFileAttributes.
UserPrincipal owner = attrs.owner();
UserPrincipal group = attrs.group();
Set perms = attrs.permissio
Files.createFile(newPath,
PosixFilePermissions.asFileAttribute(perms));
-
8/6/2019 File System API 428204
10/21
10 | 2011 Oracle Corporation
DirectoryStream
DirectoryStream to iterate over entries
Scales to large directories and uses less resources
Smooth out response time for remote file systems
Provides handle to open directory
Filtering
Built-in support for glob and regex patterns and custo DirectoryStream
Extends Iterable to allow use of for-each construct
Extends Closeable, need to remember to close stream
-
8/6/2019 File System API 428204
11/21
11 | 2011 Oracle Corporation
DirectoryStream
Path dir = ...
try (DirectoryStream stream =
Files.newDirectoryStream(dir, *.java
for (Path entry : stream) {
System.out.println(entry.getNam}
}
-
8/6/2019 File System API 428204
12/21
12 | 2011 Oracle Corporation
Symbolic Links
Optionally supported
API based on long standing Unix semantics Windows Vista or newer with NTFS
Symbolic links followed by default, some exception delete, moveTo, walkFileTree
Files.isSameFile Check if two paths reference the same file
-
8/6/2019 File System API 428204
13/21
13 | 2011 Oracle Corporation
Recursive Operations
Files.walkFileTree Internal iterator to walk a file tree from a given starting point
FileVisitor invoked for each file/directory encountered
Depth first, invoked twice for each directory (pre/post)
interface FileVisitor {
FileVisitResult preVisitDirectory(T dir, BasicFileAttribFileVisitResult visitFile(T file, BasicFileAttributes at
FileVisitResult vistFileFailed(T file, IOException ioe);
FileVisitResult postVisitDirectory(T dir, IOException io
}
-
8/6/2019 File System API 428204
14/21
14 | 2011 Oracle Corporation
Recursive Operations
Files.walkFileTree Internal iterator to walk a file tree from a given starting point FileVisitor invoked for each file/directory encountered
Depth first, invoked twice for each directory (pre/post)
Return value controls iteration, can also limit depth
Easy to develop recursive operations
SimpleFileVisitor with default behavior
Symbolic links not followed by default, use FOLLOW Cycles detected and reported
-
8/6/2019 File System API 428204
15/21
15 | 2011 Oracle Corporation
Performance
NFS/remote ZFS/local
0
10
20
30
40
50
60
70
80
Runtime(s)
Existing API
New API
FAT32/Windows
00.5
1
1.5
2
2.5
3
3.5
4
4.5
5
Runtime(s)
du -k find -name
-
8/6/2019 File System API 428204
16/21
16 | 2011 Oracle Corporation
File change notification
Watch files and directories for changes
Typically done by polling Scanning directories, checking file timestamps, ...
WatchService
Watches registered objects for events
Makes use of native event notification facility where p All providers required to support monitoring of directo
Events when files are created, deleted, or modified
Extendable to other objects and events
-
8/6/2019 File System API 428204
17/21
17 | 2011 Oracle Corporation
File change notification Registration
WatchService watcher =FileSystems.getDefault().newWatchService();
Path dir = ...
WatchKey key = dir.register(watcher, ENTRY_CREATE);
-
8/6/2019 File System API 428204
18/21
18 | 2011 Oracle Corporation
File change notification Retrieving ev
for (;;) {WatchKey key = watcher.take();
for (WatchEvent event: key.pollEvents()) {
if (event.kind() == ENTRY_CREATE) {
Path name = (Path)event.context();
System.out.format(%s created%n, name);
}
}
key.reset();
}
-
8/6/2019 File System API 428204
19/21
19 | 2011 Oracle Corporation
Service Provider Interface
FileSystemProvider is the factory for FileSystem ins
Develop and deploy custom file system implementa
Deploy as JAR file on class path or in extensions d
ZIP file system Example provider included in the JDK 7 demos
Treats contents of zip or JAR file as a file system
-
8/6/2019 File System API 428204
20/21
20 | 2011 Oracle Corporation
Summary
The JDK gets a comprehensive interface to the file
Easy to use, yet powerful and extendable
Code samples and demos available in JDK 7
-
8/6/2019 File System API 428204
21/21
21 | 2011 Oracle Corporation