More Related Content Similar to もっと New I/O。 (20) More from Yuichi Sakuraba More from Yuichi Sakuraba (20) もっと New I/O。2. JSR 51 New I/O APIs
Buffer/Direct Buffer
Channel
ノンブロッキング I/O
Charset
積み残しあり!
Mark Reinhold
3. JSR 51 New I/O APIs
An API for scalable I/O operations
Buffer/Directsockets, in the form of
on both files and Buffer
either asynchronous requests
Channel
or polling
Aノンブロッキングinterface that supports bulk access
new filesystem I/O
to file attributes (including MIME content types),
escape Charset
to filesystem-specific APIs,
and a service-provider interface for pluggable filesystem
積み残しあり!
implementations.
Mark Reinhold
4. JSR 203 More New I/O APIs
非同期 I/O
ファイルシステムインタフェース
SocketChannel
のマルチキャスト
Alan Bateman
当初 J2SE5.0 向け
6. なぜファイルシステム ?
テ ム
シ ス
イ ル
フ ァ な ら
い
メタデータ
し ー ス
新 ェ
フ シンボリックリンク
ン タ !!
イ 解 決
べ て
ファイルの監視
す
7. <<factory>>
FileSystem
FileSystems
<<interface>>
Files Path
AttributeView
<<interface>> <<interface>>
FileVisitor WatchService
8. 生成
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");
9. 生成
File file = ...;
// File から Path
Path path = file.toPath();
// Path から File
File file2 = path.toFile();
10. 生成
// シンボリックリンクのターゲット
File file
FileSystem=fileSystem = FileSystems.getDefault();
...;
Path target = fileSystem.getPath("realfile.txt");
// file.txt の生成
シンボリックリンクするファイル
File から Path
Path path1==file.toPath();
link
path fileSystem.getPath("link.txt"); null);
fileSystem.getPath("file.txt",
// foo/bar/file.txt の生成
シンボリックリンクの作成
Path から File
Files.createSymbolicLink(link, target);
File file2
Path path2 = fileSystem.getPath("foo", "bar", "file.txt");
path.toFile();
11. 操作 : Files クラス
// ファイルのコピー
String source = ...;
Path source = fileSystem.getPath(source);
String destination = ...;
Path destination = fileSystem.getPath(destination);
Files.copy(source, destination);
12. 操作 : Files クラス
Path source = ...; Path dest = ...;
// 移動
Files.move(source, dest);
// 削除
Files.delete(dest);
Files.deleteIfExists(dest);
13. 操作 : 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);
14. 操作 : Files クラス
Path source = ...;
// ファイルのコピーPath dest = ...;
path = ...;
// ストリームなどの取得
String source = ...;
簡易入出力
byte[]
BufferedReader reader = Files.newBufferedReader(path,
// 移動bytes
Path source ==fileSystem.getPath(source);
Files.readAllBytes(path);
List<String> lines = Files.readAllLines(path,
Files.move(source, dest); Charset.defaultCharset());
BufferedWriter writer = Files.newBufferedWriter(path,
String destination = ...; Charset.defaultCharset());
// 削除
Path destination = fileSystem.getPath(destination);
Charset.defaultCharset());
Files.write(path, bytes);
InputStream iStream
Files.delete(dest); = Files.newInputStream(path);
Files.write(path, destination);
OutputStream oStream = Files.newOutputStream(path);
Files.deleteIfExists(dest);
Files.copy(source,lines, Charset.defaultCharset());
ByteChannel channel = Files.newByteChannel(path);
15. メタデータ
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();
16. メタデータ
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();
17. ファイルビジター 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;
}
});
18. ファイルの監視
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();
}
19. 非同期 I/O
Selector
AsynchronousChannel
Future
CompletionHandler
マルチキャスト
DatagramChannel
20. JSR 51 の積み残し
非同期 I/O
ファイルシステムインタフェース
マルチキャスト
JSR 203 More New I/O
ファイルシステムインタフェース
メタデータ ファイルビジター
ファイルの監視