Lesson 10 Java File I/O (NIO.2) презентация

Содержание


Презентации» Образование» Lesson 10 Java File I/O (NIO.2)
Lesson 10 Java File I/O (NIO.2)Objectives
 After completing this lesson, you should be able to:
 UseNew File I/O API (NIO.2)Limitations of java.io.FileFile Systems, Paths, Files
 In NIO.2, both files and directories areRelative Path Versus Absolute Path
 A path is either relative orSymbolic LinksJava NIO.2 Concepts
 Prior to JDK 7, the java.io.File class wasPath Interface
 The java.nio.file.Path interface provides the entry point for thePath Interface Features
 The Path interface defines the methods used toPath: Example
  public class PathTest
    public staticRemoving Redundancies from a Path
 Many file systems use “.” notationCreating a Subpath
 A portion of a path can be obtainedJoining Two Paths
 The resolve method is used to combine twoCreating a Path Between Two Paths
 The relativize method enables youWorking with Links
 Path interface is “link aware.”
 Every Path method either:
 DetectsQuiz
 Given a Path object with the following path: 
 /export/home/heimer/../williams/./documents
Quiz
 Given the following path:
 Path p = Paths.get ("/home/export/tom/documents/coursefiles/JDK7");
 andQuiz
 Given this code fragment:
 Path p1 = Paths.get("D:/temp/foo/");
 Path p2File OperationsChecking a File or Directory
 A Path object represents the conceptChecking a File or Directory
 To verify that a file canCreating Files and Directories
 Files and directories can be created usingDeleting a File or Directory
 You can delete files, directories, orCopying a File or Directory
 You can copy a file orCopying Between a Stream and Path
 You may also want toMoving a File or Directory
 You can move a file orListing a Directory’s Contents
 The DirectoryStream class provides a mechanism toReading/Writing All Bytes or Lines from a File
 The readAllBytes orChannels and ByteBuffers
 Stream I/O reads a character at a time,Random Access Files
 Random access files permit non-sequential, or random, accessBuffered I/O Methods for Text Files
 The newBufferedReader method opens aByte Streams
 NIO.2 also supports methods to open byte streams.
 ToManaging MetadataFile Attributes (DOS)
 File attributes can be read from a fileDOS File Attributes: Example
  DosFileAttributes attrs = null;
  PathPOSIX Permissions
 With NIO.2, you can create files and directories onQuiz
 Given the following fragment:
 Path p1 = Paths.get("/export/home/peter");
 Path p2Quiz
 Given this fragment:
 Path source = Paths.get(args[0]);
 Path target =Quiz
 Given this fragment:
 Path source = Paths.get("/export/home/mcginn/HelloWorld.java");
 Path newdir =Recursive Operations
 The Files class provides a method to walk theFileVisitor Method OrderFileVisitor Method OrderFileVisitor Method OrderExample: WalkFileTreeExample
  Path path = Paths.get("D:/Test");
  try {
 Finding Files
 To find a file, typically, you would search aPathMatcher Syntax and Pattern
 The syntaxAndPattern string is of the form:
PathMatcher: Example
  public static void main(String[] args) {
  Finder Class
  public class Finder extends SimpleFileVisitor<Path> {
  Other Useful NIO.2 Classes
 The FileStore class is useful for providingMoving to NIO.2
 A method was added to the java.io.File classSummary
 In this lesson, you should have learned how to:
 UseQuiz
 To copy, move, or open a file or directory usingQuiz
 Given any starting directory path, which FileVisitor method(s) would youQuiz
 Given an application where you want to count the depth



Слайды и текст этой презентации
Слайд 1
Описание слайда:
Lesson 10 Java File I/O (NIO.2)


Слайд 2
Описание слайда:
Objectives After completing this lesson, you should be able to: Use the Path interface to operate on file and directory paths Use the Files class to check, delete, copy, or move a file or directory Use Files class methods to read and write files using channel I/O and stream I/O Read and change file and directory attributes Recursively access a directory tree Find a file by using the PathMatcher class

Слайд 3
Описание слайда:
New File I/O API (NIO.2)

Слайд 4
Описание слайда:
Limitations of java.io.File

Слайд 5
Описание слайда:
File Systems, Paths, Files In NIO.2, both files and directories are represented by a path, which is the relative or absolute location of the file or directory.

Слайд 6
Описание слайда:
Relative Path Versus Absolute Path A path is either relative or absolute. An absolute path always contains the root element and the complete directory list required to locate the file. Example: A relative path must be combined with another path in order to access a file. Example:

Слайд 7
Описание слайда:
Symbolic Links

Слайд 8
Описание слайда:
Java NIO.2 Concepts Prior to JDK 7, the java.io.File class was the entry point for all file and directory operations. With NIO.2, there is a new package and classes: java.nio.file.Path: Locates a file or a directory by using a system-dependent path java.nio.file.Files: Using a Path, performs operations on files and directories java.nio.file.FileSystem: Provides an interface to a file system and a factory for creating a Path and other objects that access a file system All the methods that access the file system throw IOException or a subclass.

Слайд 9
Описание слайда:
Path Interface The java.nio.file.Path interface provides the entry point for the NIO.2 file and directory manipulation. To obtain a Path object, obtain an instance of the default file system, and then invoke the getPath method: FileSystem fs = FileSystems.getDefault(); Path p1 = fs.getPath ("D:\\labs\\resources\\myFile.txt"); The java.nio.file package also provides a static final helper class called Paths to perform getDefault: Path p1 = Paths.get ("D:\\labs\\resources\\myFile.txt"); Path p2 = Paths.get ("D:", "labs", "resources", "myFile.txt"); Path p3 = Paths.get ("/temp/foo"); Path p4 = Paths.get (URI.create ("file:///~/somefile");

Слайд 10
Описание слайда:
Path Interface Features The Path interface defines the methods used to locate a file or a directory in a file system. These methods include: To access the components of a path: getFileName, getParent, getRoot, getNameCount To operate on a path: normalize, toUri, toAbsolutePath, subpath, resolve, relativize To compare paths: startsWith, endsWith, equals

Слайд 11
Описание слайда:
Path: Example public class PathTest public static void main(String[] args) { Path p1 = Paths.get(args[0]); System.out.format("getFileName: %s%n", p1.getFileName()); System.out.format("getParent: %s%n", p1.getParent()); System.out.format("getNameCount: %d%n", p1.getNameCount()); System.out.format("getRoot: %s%n", p1.getRoot()); System.out.format("isAbsolute: %b%n", p1.isAbsolute()); System.out.format("toAbsolutePath: %s%n", p1.toAbsolutePath()); System.out.format("toURI: %s%n", p1.toUri()); } } java PathTest D:/Temp/Foo/file1.txt getFileName: file1.txt getParent: D:\Temp\Foo getNameCount: 3 getRoot: D:\ isAbsolute: true toAbsolutePath: D:\Temp\Foo\file1.txt toURI: file:///D:/Temp/Foo/file1.txt

Слайд 12
Описание слайда:
Removing Redundancies from a Path Many file systems use “.” notation to denote the current directory and “..” to denote the parent directory. The following examples both include redundancies: The normalize method removes any redundant elements, which includes any “.” or “directory/..” occurrences. Example:

Слайд 13
Описание слайда:
Creating a Subpath A portion of a path can be obtained by creating a subpath using the subpath method: Path subpath(int beginIndex, int endIndex); The element returned by endIndex is one less that the endIndex value. Example: Path p1 = Paths.get ("D:/Temp/foo/bar"); Path p2 = p1.subpath (1, 3); foo\bar

Слайд 14
Описание слайда:
Joining Two Paths The resolve method is used to combine two paths. Example: Passing an absolute path to the resolve method returns the passed-in path.

Слайд 15
Описание слайда:
Creating a Path Between Two Paths The relativize method enables you to construct a path from one location in the file system to another location. The method constructs a path originating from the original path and ending at the location specified by the passed-in path. The new path is relative to the original path. Example:

Слайд 16
Описание слайда:
Working with Links Path interface is “link aware.” Every Path method either: Detects what to do when a symbolic link is encountered, or Provides an option enabling you to configure the behavior when a symbolic link is encountered

Слайд 17
Описание слайда:
Quiz Given a Path object with the following path: /export/home/heimer/../williams/./documents What Path method would remove the redundant elements? normalize relativize resolve toAbsolutePath

Слайд 18
Описание слайда:
Quiz Given the following path: Path p = Paths.get ("/home/export/tom/documents/coursefiles/JDK7"); and the statement: Path sub = p.subPath (x, y); What values for x and y will produce a Path that contains documents/coursefiles? x = 3, y = 4 x = 3, y = 5 x = 4, y = 5 x = 4, y = 6

Слайд 19
Описание слайда:
Quiz Given this code fragment: Path p1 = Paths.get("D:/temp/foo/"); Path p2 = Paths.get("../bar/documents"); Path p3 = p1.resolve(p2).normalize(); System.out.println(p3); What is the result? Compiler error IOException D:\temp\foo\documents D:\temp\bar\documents D:\temp\foo\..\bar\documents

Слайд 20
Описание слайда:
File Operations

Слайд 21
Описание слайда:
Checking a File or Directory A Path object represents the concept of a file or a directory location. Before you can access a file or directory, you should first access the file system to determine whether it exists using the following Files methods: exists(Path p, LinkOption... option) Tests to see whether a file exists. By default, symbolic links are followed. notExists(Path p, LinkOption... option) Tests to see whether a file does not exist. By default, symbolic links are followed. Example: Path p = Paths.get(args[0]); System.out.format("Path %s exists: %b%n", p, Files.exists(p, LinkOption.NOFOLLOW_LINKS));

Слайд 22
Описание слайда:
Checking a File or Directory To verify that a file can be accessed, the Files class provides the following boolean methods. isReadable(Path) isWritable(Path) isExecutable(Path) Note that these tests are not atomic with respect to other file system operations. Therefore, the results of these tests may not be reliable once the methods complete. The isSameFile (Path, Path) method tests to see whether two paths point to the same file. This is particularly useful in file systems that support symbolic links.

Слайд 23
Описание слайда:
Creating Files and Directories Files and directories can be created using one of the following methods: Files.createFile (Path dir); Files.createDirectory (Path dir); The createDirectories method can be used to create directories that do not exist, from top to bottom: Files.createDirectories(Paths.get("D:/Temp/foo/bar/example"));

Слайд 24
Описание слайда:
Deleting a File or Directory You can delete files, directories, or links. The Files class provides two methods: delete(Path) deleteIfExists(Path)

Слайд 25
Описание слайда:
Copying a File or Directory You can copy a file or directory by using the copy(Path, Path, CopyOption...) method. When directories are copied, the files inside the directory are not copied. Example:

Слайд 26
Описание слайда:
Copying Between a Stream and Path You may also want to be able to copy (or write) from a Stream to file or from a file to a Stream. The Files class provides two methods to make this easy: copy(InputStream source, Path target, CopyOption... options) copy(Path source, OutputStream out) An interesting use of the first method is copying from a web page and saving to a file: Path path = Paths.get("D:/Temp/oracle.html"); URI u = URI.create("http://www.oracle.com/"); try (InputStream in = u.toURL().openStream()) { Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING); } catch (final MalformedURLException | IOException e) { System.out.println("Exception: " + e); }

Слайд 27
Описание слайда:
Moving a File or Directory You can move a file or directory by using the move(Path, Path, CopyOption...) method. Moving a directory will not move the contents of the directory. Example:

Слайд 28
Описание слайда:
Listing a Directory’s Contents The DirectoryStream class provides a mechanism to iterate over all the entries in a directory. Path dir = Paths.get("D:/Temp"); // DirectoryStream is a stream, so use try-with-resources // or explicitly close it when finished try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.zip")) { for (Path file : stream) { System.out.println(file.getFileName()); } } catch (PatternSyntaxException | DirectoryIteratorException | IOException x) { System.err.println(x); } DirectoryStream scales to support very large directories.

Слайд 29
Описание слайда:
Reading/Writing All Bytes or Lines from a File The readAllBytes or readAllLines method reads entire contents of the file in one pass. Example: Use write method(s) to write bytes, or lines, to a file.

Слайд 30
Описание слайда:
Channels and ByteBuffers Stream I/O reads a character at a time, while channel I/O reads a buffer at a time. The ByteChannel interface provides basic read and write functionality. A SeekableByteChannel is a ByteChannel that has the capability to maintain a position in the channel and to change that position. The two methods for reading and writing channel I/O are: The capability to move to different points in the file and then read from or write to that location makes random access of a file possible.

Слайд 31
Описание слайда:
Random Access Files Random access files permit non-sequential, or random, access to a file’s contents. To access a file randomly, open the file, seek a particular location, and read from or write to that file. Random access functionality is enabled by the SeekableByteChannel interface.

Слайд 32
Описание слайда:
Buffered I/O Methods for Text Files The newBufferedReader method opens a file for reading. The newBufferedWriter method writes to a file using a BufferedWriter.

Слайд 33
Описание слайда:
Byte Streams NIO.2 also supports methods to open byte streams. To create a file, append to a file, or write to a file, use the newOutputStream method.

Слайд 34
Описание слайда:
Managing Metadata

Слайд 35
Описание слайда:
File Attributes (DOS) File attributes can be read from a file or directory in a single call: DosFileAttributes attrs = Files.readAttributes (path, DosFileAttributes.class); DOS file systems can modify attributes after file creation: Files.createFile (file); Files.setAttribute (file, "dos:hidden", true);

Слайд 36
Описание слайда:
DOS File Attributes: Example DosFileAttributes attrs = null; Path file = ...; try { attrs = Files.readAttributes(file, DosFileAttributes.class); } catch (IOException e) { ///... } FileTime creation = attrs.creationTime(); FileTime modified = attrs.lastModifiedTime(); FileTime lastAccess = attrs.lastAccessTime(); if (!attrs.isDirectory()) { long size = attrs.size(); } // DosFileAttributes adds these to BasicFileAttributes boolean archive = attrs.isArchive(); boolean hidden = attrs.isHidden(); boolean readOnly = attrs.isReadOnly(); boolean systemFile = attrs.isSystem();

Слайд 37
Описание слайда:
POSIX Permissions With NIO.2, you can create files and directories on POSIX file systems with their initial permissions set. Path p = Paths.get(args[0]); Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rwxr-x---"); FileAttribute<Set<PosixFilePermission>> attrs = PosixFilePermissions.asFileAttribute(perms); try { Files.createFile(p, attrs); } catch (FileAlreadyExistsException f) { System.out.println("FileAlreadyExists" + f); } catch (IOException i) { System.out.println("IOException:" + i); }

Слайд 38
Описание слайда:
Quiz Given the following fragment: Path p1 = Paths.get("/export/home/peter"); Path p2 = Paths.get("/export/home/peter2"); Files.move(p1, p2, StandardCopyOption.REPLACE_EXISTING); If the peter2 directory does not exist, and the peter directory is populated with subfolders and files, what is the result? DirectoryNotEmptyException NotDirectoryException Directory peter2 is created. Directory peter is copied to peter2. Directory peter2 is created and populated with files and directories from peter.

Слайд 39
Описание слайда:
Quiz Given this fragment: Path source = Paths.get(args[0]); Path target = Paths.get(args[1]); Files.copy(source, target); Assuming source and target are not directories, how can you prevent this copy operation from generating FileAlreadyExistsException? Delete the target file before the copy. Use the move method instead. Use the copyExisting method instead. Add the REPLACE_EXISTING option to the method.

Слайд 40
Описание слайда:
Quiz Given this fragment: Path source = Paths.get("/export/home/mcginn/HelloWorld.java"); Path newdir = Paths.get("/export/home/heimer"); Files.copy(source, newdir.resolve(source.getFileName()); Assuming there are no exceptions, what is the result? The contents of mcginn are copied to heimer. HelloWorld.java is copied to /export/home. HelloWorld.java is coped to /export/home/heimer. The contents of heimer are copied to mcginn.

Слайд 41
Описание слайда:
Recursive Operations The Files class provides a method to walk the file tree for recursive operations, such as copies and deletes. walkFileTree (Path start, FileVisitor<T>) Example: public class PrintTree implements FileVisitor<Path> { public FileVisitResult preVisitDirectory(Path, BasicFileAttributes){} public FileVisitResult postVisitDirectory(Path, BasicFileAttributes){} public FileVisitResult visitFile(Path, BasicFileAttributes){} public FileVisitResult visitFileFailed(Path, BasicFileAttributes){} } public class WalkFileTreeExample { public printFileTree(Path p) { Files.walkFileTree(p, new PrintTree()); } }

Слайд 42
Описание слайда:
FileVisitor Method Order

Слайд 43
Описание слайда:
FileVisitor Method Order

Слайд 44
Описание слайда:
FileVisitor Method Order

Слайд 45
Описание слайда:
Example: WalkFileTreeExample Path path = Paths.get("D:/Test"); try { Files.walkFileTree(path, new PrintTree()); } catch (IOException e) { System.out.println("Exception: " + e); }

Слайд 46
Описание слайда:
Finding Files To find a file, typically, you would search a directory. You could use a search tool, or a command, such as: dir /s *.java This command will recursively search the directory tree, starting from where you are for all files that contain the java extension. The java.nio.file.PathMatcher interface includes a match method to determine whether a Path object matches a specified search string. Each file system implementation provides a PathMatcher that can be retrieved by using the FileSystems factory: PathMatcher matcher = FileSystems.getDefault().getPathMatcher (String syntaxAndPattern);

Слайд 47
Описание слайда:
PathMatcher Syntax and Pattern The syntaxAndPattern string is of the form: syntax:pattern Where syntax can be “glob” and “regex”. The glob syntax is similar to regular expressions, but simpler:

Слайд 48
Описание слайда:
PathMatcher: Example public static void main(String[] args) { // ... check for two arguments Path root = Paths.get(args[0]); // ... check that the first argument is a directory PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + args[1]); // Finder is class that implements FileVisitor Finder finder = new Finder(root, matcher); try { Files.walkFileTree(root, finder); } catch (IOException e) { System.out.println("Exception: " + e); } finder.done(); }

Слайд 49
Описание слайда:
Finder Class public class Finder extends SimpleFileVisitor<Path> { private Path file; private PathMatcher matcher; private int numMatches; // ... constructor stores Path and PathMatcher objects private void find(Path file) { Path name = file.getFileName(); if (name != null && matcher.matches(name)) { numMatches++; System.out.println(file); } } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { find(file); return CONTINUE; } //... }

Слайд 50
Описание слайда:
Other Useful NIO.2 Classes The FileStore class is useful for providing usage information about a file system, such as the total, usable, and allocated disk space. Filesystem kbytes used avail System (C:) 209748988 72247420 137501568 Data (D:) 81847292 429488 81417804 An instance of the WatchService interface can be used to report changes to registered Path objects. WatchService can be used to identify when files are added, deleted, or modified in a directory. ENTRY_CREATE: D:\test\New Text Document.txt ENTRY_CREATE: D:\test\Foo.txt ENTRY_MODIFY: D:\test\Foo.txt ENTRY_MODIFY: D:\test\Foo.txt ENTRY_DELETE: D:\test\Foo.txt

Слайд 51
Описание слайда:
Moving to NIO.2 A method was added to the java.io.File class for JDK 7 to provide forward compatibility with NIO.2. Path path = file.toPath(); This enables you to take advantage of NIO.2 without having to rewrite a lot of code. Further, you could replace your existing code to improve future maintenance—for example, replace file.delete(); with: Path path = file.toPath(); Files.delete (path); Conversely, the Path interface provides a method to construct a java.io.File object: File file = path.toFile();

Слайд 52
Описание слайда:
Summary In this lesson, you should have learned how to: Use the Path interface to operate on file and directory paths Use the Files class to check, delete, copy, or move a file or directory Use Files class methods to read and write files using channel I/O and stream I/O Read and change file and directory attributes Recursively access a directory tree Find a file by using the PathMatcher class

Слайд 53
Описание слайда:
Quiz To copy, move, or open a file or directory using NIO.2, you must first create an instance of: Path Files FileSystem Channel

Слайд 54
Описание слайда:
Quiz Given any starting directory path, which FileVisitor method(s) would you use to delete a file tree? preVisitDirectory() postVisitDirectory() visitFile() visitDirectory()

Слайд 55
Описание слайда:
Quiz Given an application where you want to count the depth of a file tree (how many levels of directories), which FileVisitor method should you use? preVisitDirectory() postVisitDirectory() visitFile() visitDirectory()


Скачать презентацию на тему Lesson 10 Java File I/O (NIO.2) можно ниже:

Похожие презентации