Featuring JDK 7 Nio 2

1,665 views

Published on

Edge Dalmacio, Co-Founder of Haybol.ph and Senior Software Engineer at Orange and Bronze, discusses JDK 7 Nio 2 at the PinoyJUG Java 7 Release Party.

www.orangeandbronze.com

Published in: Technology, Education
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,665
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
37
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Featuring JDK 7 Nio 2

  1. 1. Featuring JDK 7 NIO 2<br />@edgedalmacio<br />about.me/edgedalmacio<br />haybol.ph<br />
  2. 2. THANK YOU<br />
  3. 3. java.io.File<br />Many methods didn't throw exceptions when they failed.<br />Rename method didn't work consistently across platforms.<br />There was no real support for symbolic links.<br />Limited support for metadata, such as file permissions, file owner, and other security attributes.<br />Accessing file metadata was inefficient.<br />Many of the File methods didn't scale.<br />It was not possible to write reliable code that could recursively walk a file tree and respond appropriately if there were circular symbolic links.<br />
  4. 4. java.nio.file.Path<br />
  5. 5. Creating a Path<br />import java.nio.file.Paths;<br />Path p1 = Paths.get("/tmp/foo"); <br />Path p2 = Paths.get(args[0]); <br />Path p3 = Paths.get(<br />URI.create("file:///Users/joe/FileTest.java"));<br />
  6. 6. Checking File Accessibility<br />import java.nio.file.Files;<br />Path file = ...; <br />booleanisRegularExecutableFile<br /> = Files.isRegularFile(file) <br /> & Files.isReadable(file) <br /> & Files.isExecutable(file);<br />
  7. 7. Checking Whether Two Paths Locate the Same File<br />Path p1 = ...; <br />Path p2 = ...; <br />if (Files.isSameFile(p1, p2)) { <br />//Logic when the paths locate the same file <br />}<br />
  8. 8. Deleting a File or Directory<br />try { Files.delete(path); } <br />catch <br />(NoSuchFileException) …<br />catch<br />(DirectoryNotEmptyException) …<br />catch <br />(IOException) …<br />
  9. 9. Copying a File or Directory<br />import static java.nio.file.StandardCopyOption.*; <br />… <br />Files.copy(source, target, REPLACE_EXISTING,<br />COPY_ATTRIBUTES,<br />NOFOLLOW_LINKS);<br />
  10. 10. Managing Metadata<br />java.nio.file.Files<br />size<br />isDirectory<br />isRegularFile<br />isSymbolicLink<br />isHidden<br />get/setLastModifiedTime<br />get/setOwner<br />get/setPosixFilePermissions<br />get/setAttribute<br />
  11. 11. Reading, Writing and Creating Files<br />
  12. 12. Reading a File by Using Buffered Stream I/O<br />BufferedReaderreader = Files.newBufferedReader<br /> (file, charset)<br />reader.readLine();<br />
  13. 13. Methods for Unbuffered Streams and Interoperable with java.io APIs<br />InputStreamin = Files.newInputStream(file); <br />BufferedReaderreader = <br />new BufferedReader(<br /> new InputStreamReader(in));<br />reader.readLine();<br />
  14. 14. Interoperability With Legacy Code<br />java.io.Filefile = <br /> new File(pathname);<br />java.nio.file.Path path = <br />file.toPath();<br />
  15. 15. java.nio.file<br />Path<br />methods for manipulating a path.<br />Files<br />methods for file operations, such as moving, copy, deleting, and also methods for retrieving and setting file attributes.<br />FileSystem<br />methods for obtaining information about the file system.<br />
  16. 16. About Orange & Bronze Software Labs<br /><ul><li>Filipino software development company specializing in Java technology
  17. 17. Consulting, outsourcing, and offshore product development services using Open Source technologies, with a specialization in the Spring and Grails frameworks
  18. 18. Enterprise solutions with Google enterprise products and business intelligence solutions with the Pentaho BI Suite
  19. 19. Offers Java, Agile and Android training courses</li></ul>www.orangeandbronze.com<br />
  20. 20. THANK YOU<br />
  21. 21. References<br />File I/O (Featuring NIO.2)<br />http://download.oracle.com/javase/tutorial/essential/io/fileio.html<br />More New I/O APIs for the Java Platform<br />http://openjdk.java.net/projects/nio/<br />

×