NIO.2を使って簡単ファイル操作
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

NIO.2を使って簡単ファイル操作

  • 3,600 views
Uploaded on

JavaOne Tokyo 2012...

JavaOne Tokyo 2012
JS1-04 NIO.2を使って、簡単ファイル操作

More in: Technology , Design
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
3,600
On Slideshare
3,156
From Embeds
444
Number of Embeds
5

Actions

Shares
Downloads
9
Comments
0
Likes
5

Embeds 444

http://www.javainthebox.com 338
http://cappuccino.jp 98
http://cappuccino.ne.jp 4
https://twitter.com 3
http://www.cappuccino.jp 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. NIO.2 を使って、簡単ファイル操作 Java in the Box 櫻庭 祐一 #jt12_s104
  • 2. Agenda JSR 51 New I/O APIsJSR 203 More New I/O APIs New Filesystem API Conclusion
  • 3. JSR 51 New I/O APIs Buffer/Direct Buffer ChannelNon-Blocking I/O Charset Regular Expression Mark Reinhold
  • 4. JSR 51 New I/O APIs Buffer/Direct Buffer り ! あ Channel し 残Non-Blocking I/O み積 Charset Regular Expression Mark Reinhold
  • 5. An API for scalable I/O operations on both files and sockets, in the form of either asynchronous requests or pollingA new filesystem interface that supportsbulk access to file attributes (including MIME content types),escape to filesystem-specific APIs,and a service-provider interface for pluggable filesystemimplementations. Mark Reinhold
  • 6. JSR 203 More New I/O APIs Asynchronous I/O New Filesystem InterfaceMulticast with SocketChannel Alan Bateman
  • 7. コピーなどの基本操作 Why Now?foo.txt作成日などのメタデータ ディレクトリの探索
  • 8. <<factory>>FileSystems <<interface>>FileSystem Path Files Basic Classes
  • 9. Instantiation// 物理的なファイルシステムFileSystem fileSystem = FileSystems.getDefault();// foo.txt に対応するパスの生成Path foo = fileSystem.getPath("foo.txt");// baz/bar/foo.txt に対応するパスの生成Path foo2 = fileSystem.getPath("baz", "bar", "foo.txt");// foo.txt に対応するパスの簡易的な生成Path foo3 = Paths.get("foo.txt");
  • 10. InstantiationFileSystem fileSystem = FileSystems.getDefault();Path jarfile = fileSystem.getPath("someapp.jar");// someapp.jar に対応する FileSystem オブジェクトを取得するFileSystem jarFileSystem = FileSystems.newFileSystem(jarfile, ClassLoader.getSystemClassLoader()); << 処理 >>// クローズjarFileSystem.close();
  • 11. ConversionFile file = ...;// File から PathPath path = file.toPath();// Path から FileFile file2 = path.toFile();
  • 12. Creation// 空のファイルを生成Path path = fileSystem.getPath("foo.txt");Files.createFile(path);// ディレクトリを生成Path dir = fileSystem.getPath("bar");Files.createDirectory(dir);// ディレクトリツリーを生成Path dir2 = fileSystem.getPath("baz", "quux");Files.createDirectories(dir2);
  • 13. Link Creation// ハードリンクの作成Path target = fileSystem.getPath("foo.txt");Path link = fileSystem.getPath("bar.txt");Files.createLink(link, target);// シンボリックリンクの生成Path symLink = fileSystem.getPath("baz.txt");Files.createSymbolicLink(symLink, target);
  • 14. OperationsPath src = fileSystem.getPath(...);Path dest = fileSystem.getPath(...);// File CopyFiles.copy(src, dest);// MoveFiles.move(src, dest);// DeleteFiles.delete(dest);Files.deleteIfExists(dest);
  • 15. Input/OutputPath path = ...; Charset charset = ...;// Reader/WirterBufferedReader reader = Files.newBufferedReader(path, charset);BufferedWriter writer = Files.newBufferedWriter(path, charset);// StreamInputStream iStream = Files.newInputStream(path);OutputStream oStream = Files.newOutputStream(path);// ChannelSeekableByteChannel channel = Files.newByteChannel(path);
  • 16. Simple Input/OutputPath path = ...; Charset charset = ...;// 簡易入力byte[] bytes = Files.readAllBytes(path);List<String> lines = Files.readAllLines(path, charset);// 簡易出力Files.write(path, bytes);Files.write(path, lines, charset);
  • 17. Metadata foo.txt
  • 18. Two ways for dealing with Metadata Calling Files methods Through AttributeView interface
  • 19. MetadataPath path = ...;// オーナーUserPrincipal owner = Files.getOwner(path);// 最終更新日時FileTime modifyDate = Files.getLastModifiedTime(path);// 作成日時FileTime creatDate = (FileTime)Files.getAttribute(path, "creationTime");
  • 20. AttributeView <<interface>> <<interface>> AttributeView FileAttributeView <<interface>>BasicFileAttributeView <<interface>> AclFileAttributeView <<interface>> FileOwnerAttributeView <<interface>> <<interface>>DosFileAttributeView PosixFileAttributeView
  • 21. AttributeViewPath path = ...;BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);// アトリビュートを保持するのは BasicFileAttributeBasicFileAttributes attributes = view.readAttributes();// アトリビュートの取得FileTime creationTime = attributes.creationTime();FileTime lastAccessTime = attributes.lastAccessTime();
  • 22. Walk ThroughDirectory Treeby Visitor Pattern
  • 23. <<interface>> FileVisitorpreVisitDirectorypostVisitDirectoryvisitFilevisitFileFailed
  • 24. 1. preVisitDirectory alpha 11. postVisitDirectory 2. preVisitDirectory bravo 8. preVisitDirectory charlie 10. postVisitDirectory 7. postVisitDirectory3. visitFile bar.txt delta baz.txt 9. visitFile 4. preVisitDirectory 6. postVisitDirectory <<interface>> foo.txt FileVisitor 5. visitFile preVisitDirectory postVisitDirectory visitFile visitFileFailed
  • 25. File SearchPath startDir = Paths.get(".");final String regex = "Test.java";Files.walkFileTree(startDir, new SimpleFileVisitor<Path>() { public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) { if (Pattern.matches(regex, path.getFileName().toString())) { System.out.println("File: " + path); } return FileVisitResult.CONTINUE; }});
  • 26. Directory Watch File/Directory Creation Delete Update
  • 27. Directory WatchPath targetDir = fileSystem.getPath(...);// WatchService オブジェクトの生成WatchService watcher = fileSystem.newWatchService();// ディレクトリに WatchService オブジェクトを登録targetDir.register(watcher, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE, StandardWatchEventKinds.ENTRY_MODIFY);
  • 28. Directory Watchwhile(...) { // 現在のイベントを取得 WatchKey key = watcher.take(); for (WatchEvent<?> event: key.pollEvents()) { // イベントの種類と対象となるパスを出力 System.out.println(event.kind() + " " + event.context()); } key.reset();}
  • 29. Conclusion JSR 203 More New I/O New Filesystem Interface Asyn I/O MulticastNew Filesystem Interface foo.txt
  • 30. References ITpro Java SE 7 徹底理解第 4 回 New I/O 2 の 新しいファイルシステムインタフェース その 1 http://itpro.nikkeibp.co.jp/article/COLUMN/20110725/362803/第 5 回 New I/O 2 の 新しいファイルシステムインタフェース その 2 http://itpro.nikkeibp.co.jp/article/COLUMN/20110830/367909/第 6 回 New I/O 2 で非同期 I/O http://itpro.nikkeibp.co.jp/article/COLUMN/20110927/369451/
  • 31. 使っ て、NIO .2 を 操作 ァイ ル 簡単 フ eB ox in th Ja va 一 庭 祐 櫻