The paths class provides a static get() method to find a valid path. true or false?
The Show
This section covers the following:
Creating a PathA You can easily create a Path p1 = Paths.get("/tmp/foo"); Path p2 = Paths.get(args[0]); Path p3 = Paths.get(URI.create("file:///Users/joe/FileTest.java")); The Path p4 = FileSystems.getDefault().getPath("/users/sally"); The following example creates Path p5 = Paths.get(System.getProperty("user.home"),"logs", "foo.log"); Retrieving Information about a PathYou can think of the The examples in this lesson use the following directory structure. Sample Directory Structure The following code snippet defines a // None of these methods requires that the file corresponding // to the Path exists. // Microsoft Windows syntax Path path = Paths.get("C:\\home\\joe\\foo"); // Solaris syntax Path path = Paths.get("/home/joe/foo"); System.out.format("toString: %s%n", path.toString()); System.out.format("getFileName: %s%n", path.getFileName()); System.out.format("getName(0): %s%n", path.getName(0)); System.out.format("getNameCount: %d%n", path.getNameCount()); System.out.format("subpath(0,2): %s%n", path.subpath(0,2)); System.out.format("getParent: %s%n", path.getParent()); System.out.format("getRoot: %s%n", path.getRoot()); Here is the output for both Windows and the Solaris OS:
The previous example shows the output for an absolute path. In the following example, a relative path is specified: // Solaris syntax Path path = Paths.get("sally/bar"); or // Microsoft Windows syntax Path path = Paths.get("sally\\bar"); Here is the output for Windows and the Solaris OS:
Removing Redundancies From a PathMany file systems use "." notation to denote the current directory and ".." to denote the parent directory. You might have a situation where a The following examples both include redundancies: /home/./joe/foo /home/sally/../joe/foo The It is important to note that To clean up a path while
ensuring that the result locates the correct file, you can use the Converting a PathYou can use three methods to convert the Path p1 = Paths.get("/home/logfile"); // Result is file:///home/logfile System.out.format("%s%n", p1.toUri()); The public class FileTest { public static void main(String[] args) { if (args.length < 1) { System.out.println("usage: FileTest file"); System.exit(-1); } // Converts the input string to a Path object. Path inputPath = Paths.get(args[0]); // Converts the input Path // to an absolute path. // Generally, this means prepending // the current working // directory. If this example // were called like this: // java FileTest foo // the getRoot and getParent methods // would return null // on the original "inputPath" // instance. Invoking getRoot and // getParent on the "fullPath" // instance returns expected values. Path fullPath = inputPath.toAbsolutePath(); } } The The
This method throws an exception if the file does not exist or cannot be accessed. You can catch the exception when you want to handle any of these cases. For example: try { Path fp = path.toRealPath(); } catch (NoSuchFileException x) { System.err.format("%s: no such" + " file or directory%n", path); // Logic for case when file doesn't exist. } catch (IOException x) { System.err.format("%s%n", x); // Logic for other sort of file error. } Joining Two PathsYou can combine paths by using the For example, consider the following code snippet: // Solaris Path p1 = Paths.get("/home/joe/foo"); // Result is /home/joe/foo/bar System.out.format("%s%n", p1.resolve("bar")); or // Microsoft Windows Path p1 = Paths.get("C:\\home\\joe\\foo"); // Result is C:\home\joe\foo\bar System.out.format("%s%n", p1.resolve("bar")); Passing an absolute path to the // Result is /home/joe Paths.get("foo").resolve("/home/joe"); Creating a Path Between Two PathsA common requirement when you are writing file I/O code is the capability to construct a path from one location in the file system to another location. You can meet this using the For example, consider two relative paths defined as Path p1 = Paths.get("joe"); Path p2 = Paths.get("sally"); In the absence of any other information, it is assumed that // Result is ../sally Path p1_to_p2 = p1.relativize(p2); // Result is ../joe Path p2_to_p1 = p2.relativize(p1); Consider a slightly more complicated example: Path p1 = Paths.get("home"); Path p3 = Paths.get("home/sally/bar"); // Result is sally/bar Path p1_to_p3 = p1.relativize(p3); // Result is ../.. Path p3_to_p1 = p3.relativize(p1); In this example, the two paths share the same node, A relative path cannot be constructed if only one of the paths includes a root element. If both paths include a root element, the capability to construct a relative path is system dependent. The recursive
Comparing Two PathsThe Path path = ...; Path otherPath = ...; Path beginning = Paths.get("/home"); Path ending = Paths.get("foo"); if (path.equals(otherPath)) { // equality logic here } else if (path.startsWith(beginning)) { // path begins with "/home" } else if (path.endsWith(ending)) { // path ends with "foo" } The Path path = ...; for (Path name: path) { System.out.println(name); } The You can also put When you want to
verify that two Which of the following static method is not provided by the files class to check file properties or duplication?Which of the following static methods is not provided by the Files class to check file properties or duplication? Files. isArchived(Path p);
What class is the split () method a member of?The Split() method is part of the string class in C#. The method is used to split a string based on the delimiters passed to the string. The delimiters can be a character, an array of characters, or even an array of strings.
Which statement determine that Java is a directory?isDirectory() method would return true only if the file exists and it is an directory. If the file given in the path does not exists then also it return false. So it isDirectory() would return false if the path given does not exists or it exists but it is not a directory...
Which of the following is an absolute Windows path?An absolute path always contains the root element and the complete directory list required to locate the file. For example, /home/sally/statusReport is an absolute path.
|