もっと new i/o。
DESCRIPTION
TRANSCRIPT
もっと New I/O。
Java in the Box
櫻庭 祐一
JSR 51 New I/O APIs
Mark Reinhold
Buffer/Direct Buffer
Channel
ノンブロッキング I/O
Charset
積み残しあり!
JSR 51 New I/O APIs
Mark Reinhold
Buffer/Direct Buffer
Channel
ノンブロッキング I/O
Charset
積み残しあり!
An API for scalable I/O operations
on both files and sockets, in the form of
either
or polling
asynchronous requests
that supports bulk access
to file attributes (including MIME content types),
escape to filesystem-specific APIs,
and a service-provider interface for pluggable filesystem
implementations.
that supports bulk access
to file attributes (including MIME content types),
escape to filesystem-specific APIs,
and a service-provider interface for pluggable filesystem
implementations.
A new filesystem interface
JSR 203 More New I/O APIs
Alan Bateman
非同期 I/O
SocketChannel のマルチキャスト
ファイルシステムインタフェース
当初 J2SE5.0 向け
なぜファイルシステム ?
メタデータメタデータ
シンボリックリンクシンボリックリンク
ファイルの監視ファイルの監視
なぜファイルシステム ?
メタデータメタデータ
シンボリックリンクシンボリックリンク
ファイルの監視ファイルの監視
新しいファイルシステム
インタフェースなら
すべて解決 !!
<<factory>>FileSystems FileSystem
PathFiles <<interface>>AttributeView
<<interface>>FileVisitor
<<interface>>WatchService
生成
FileSystem fileSystem = FileSystems.getDefault();
// file.txt の生成
Path path1 = fileSystem.getPath("file.txt", null);
// foo/bar/file.txt の生成
Path path2 = fileSystem.getPath("foo", "bar", "file.txt");
生成
File file = ...;
// File から Path
Path path = file.toPath();
// Path から File
File file2 = path.toFile();
生成
FileSystem fileSystem = FileSystems.getDefault();
// file.txt の生成
Path path1 = fileSystem.getPath("file.txt", null);
// foo/bar/file.txt の生成
Path path2 = fileSystem.getPath("foo", "bar", "file.txt");
File file = ...;
// File から Path
Path path = file.toPath();
// Path から File
File file2 = path.toFile();
// シンボリックリンクのターゲット
Path target = fileSystem.getPath("realfile.txt");
// シンボリックリンクするファイル
Path link = fileSystem.getPath("link.txt");
// シンボリックリンクの作成
Files.createSymbolicLink(link, target);
操作 : Files クラス
// ファイルのコピー
String source = ...;
Path source = fileSystem.getPath(source);
String destination = ...;
Path destination = fileSystem.getPath(destination);
Files.copy(source, destination);
操作 : Files クラス
Path source = ...; Path dest = ...;
// 移動
Files.move(source, dest);
// 削除
Files.delete(dest);
Files.deleteIfExists(dest);
操作 : Files クラス
Path path = ...;
// ストリームなどの取得
BufferedReader reader = Files.newBufferedReader(path,
Charset.defaultCharset());
BufferedWriter writer = Files.newBufferedWriter(path,
Charset.defaultCharset());
InputStream iStream = Files.newInputStream(path);
OutputStream oStream = Files.newOutputStream(path);
ByteChannel channel = Files.newByteChannel(path);
操作 : Files クラス
// ファイルのコピー
String source = ...;
Path source = fileSystem.getPath(source);
String destination = ...;
Path destination = fileSystem.getPath(destination);
Files.copy(source, destination);
Path source = ...; Path dest = ...;
// 移動
Files.move(source, dest);
// 削除
Files.delete(dest);
Files.deleteIfExists(dest);
Path path = ...;
// ストリームなどの取得
BufferedReader reader = Files.newBufferedReader(path,
Charset.defaultCharset());
BufferedWriter writer = Files.newBufferedWriter(path,
Charset.defaultCharset());
InputStream iStream = Files.newInputStream(path);
OutputStream oStream = Files.newOutputStream(path);
ByteChannel channel = Files.newByteChannel(path);
Path path = ...;
// 簡易入出力
byte[] bytes = Files.readAllBytes(path);
List<String> lines = Files.readAllLines(path,
Charset.defaultCharset());
Files.write(path, bytes);
Files.write(path, lines, Charset.defaultCharset());
メタデータ
Path path = ...;
// 直接取得
FileTime creationTime
= (FileTime)Files.getAttribute(path, "creationTime");
// AttributeView を介して取得
BasicFileAttributeView view
= Files.getFileAttributeView(path,
BasicFileAttributeView.class);
BasicFileAttributes attributes = view.readAttributes();
FileTime lastAccessTime = attributes.lastAccessTime();
Path path = ...;
// 直接取得
FileTime creationTime
= (FileTime)Files.getAttribute(path, "creationTime");
// AttributeView を介して取得
BasicFileAttributeView view
= Files.getFileAttributeView(path,
BasicFileAttributeView.class);
BasicFileAttributes attributes = view.readAttributes();
FileTime lastAccessTime = attributes.lastAccessTime();
メタデータ
Path path = ...;
// 直接取得
FileTime creationTime
= (FileTime)Files.getAttribute(path, "creationTime");
// AttributeView を介して取得
BasicFileAttributeView view
= Files.getFileAttributeView(path,
BasicFileAttributeView.class);
BasicFileAttributes attributes = view.readAttributes();
FileTime lastAccessTime = attributes.lastAccessTime();
Path path = ...;
// 直接取得
FileTime creationTime
= (FileTime)Files.getAttribute(path, "creationTime");
// AttributeView を介して取得
BasicFileAttributeView view
= Files.getFileAttributeView(path,
BasicFileAttributeView.class);
BasicFileAttributes attributes = view.readAttributes();
FileTime lastAccessTime = attributes.lastAccessTime();
ファイルビジター Vistor パターンを使用してファイルツリーを探索
Path startDir = ...;
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
System.out.println("Visit File: " + file);
return FileVisitResult.CONTINUE;
}
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) throws IOException {
System.out.println("Visit Directory: " + dir);
return FileVisitResult.CONTINUE;
}
});
ファイルの監視WatchService
Path dir = fileSystem.getPath(...);
WatchService service = fileSystem.newWatchService();
dir.register(service,
StandardWatchEventKind.ENTRY_MODIFY);
for (;;) {
WatchKey key = service.take();
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println(event.kind()
+ " " + event.context());
}
key.reset();
}
Path dir = fileSystem.getPath(...);
WatchService service = fileSystem.newWatchService();
dir.register(service,
StandardWatchEventKind.ENTRY_MODIFY);
for (;;) {
WatchKey key = service.take();
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println(event.kind()
+ " " + event.context());
}
key.reset();
}
Path dir = fileSystem.getPath(...);
WatchService service = fileSystem.newWatchService();
dir.register(service,
StandardWatchEventKind.ENTRY_MODIFY);
for (;;) {
WatchKey key = service.take();
for (WatchEvent<?> event : key.pollEvents()) {
System.out.println(event.kind()
+ " " + event.context());
}
key.reset();
}
非同期 I/O
Selector
AsynchronousChannelFuture
CompletionHandler
マルチキャスト
DatagramChannel
JSR 51 の積み残し
非同期 I/O
ファイルシステムインタフェース
マルチキャスト
JSR 203 More New I/O
ファイルシステムインタフェース
メタデータ ファイルビジター
ファイルの監視
もっと New I/O。
Java in the Box
櫻庭 祐一