In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Java SE 7 file operation in the path operation is how, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
There are two main types of operations on Path classes in Java SE 7: operations on paths and operations on files. Let's take a look at the operation of the path.
Create a Path instance
The Path instance contains information about the location of the specified file or directory, and you need to specify one or more directory or file names when instantiating the Path class. The root directory of the path is not required; the path information may simply be the name of a directory or file.
The easiest way to create an Path instance is to use the get method of the Paths class (note that there is an s here):
Path p1 = Paths.get ("/ tmp/foo"); Path p2 = Paths.get (args [0]); Path p3 = Paths.get ("file:///Users/joe/FileTest.java");")
The Path class accepts either String or URI as a parameter.
Get path information
As we said earlier, File System is generally a tree structure, so we can think of Path as a series of names (directory names and file names) that are stored sequentially. The directory name of the * layer in the directory structure is the one with index 0 in the sequence, and the directory name or file name of the * * layer in the directory structure is the one in the sequence where index is nMel 1 (where n is the number of layers in the path). The Path class provides methods to get an element or a subsequence of a sequence through index.
The directory structure we use in the following example is shown below:
The following code defines a Path object and gets the information in it. Note that except for the isHidden method, the other methods in this code do not need the specified directory or file to exist; if not, the isHidden method throws an exception.
Java code
Path path = Paths.get ("C:\ home\\ joe\\ foo"); / / Microsoft Windows syntax / / Path path = Paths.get ("/ home/joe/foo"); / / Solaris syntax System.out.format ("toString:% s% n", path.toString ()); System.out.format ("getName:% s% n", path.getName ()) System.out.format ("getName (0):% s% n", path.getName (0)); System.out.format ("getNameCount:% d% n", path.getNameCount ()); System.out.format ("subpath (0Magne2):% d% n", path.subpath (0Magne2)); System.out.format ("getParent:% s% n", path.getParent ()) System.out.format ("getRoot:% s% n", path.getRoot ()); System.out.format ("isHidden:% s% n", path.isHidden ())
Here is the output of this code
In the above code, we created the Path using an absolute path. Let's take a look at the execution result of this code when creating a path using a relative path:
Java code
/ / Path path = Paths.get ("sally/bar"); / / Solaris syntax Path path = Paths.get ("sally\\ bar"); / / Microsoft Windows syntax
You can experiment on what the specific output is.
Remove redundancy in Path
We use'.'in many file systems. To represent the current directory, use'..' Represents the parent directory. In some cases, the path we create will have redundant path information, such as:
/ home/./joe/foo
/ home/sally/../joe/foo
The method normalize removes this redundant information, including'.' Or 'directory/..'. Both of the above examples are / home/joe/foo after removing redundant information.
Note that the normalize method does not check the file system, it simply performs syntax operations. In the second example, if sally is a symbolic link to another directory, sally/.. is removed Later may cause Path to no longer point to the original file or directory.
If you need to clear the redundant information and make sure that the result still points to the correct file or directory, you can use the toRealPath method. We will talk about this method below.
Convert Path
There are three ways to convert Path.
* toUri method
If you need to convert Path to a string format that can be opened in a browser, you can use the toUri method, for example:
Java code
Path p1 = Paths.get ("/ home/logfile"); System.out.format ("% s% n", p1.toUri ()); / / the result is file:///home/logfile
Note here that this code can be executed successfully even if the directory or file pointed to by / home/logfile' does not exist.
* toAbsolutePath method
This method converts the path to an absolute path. If the original Path is already an absolute path, the method directly returns the original Path object.
Let's look at the following example:
Java code
Path path = Paths.get ("home\ joe\ foo"); Path absolutePath = path.toAbsolutePath (); System.out.println (path = = absolutePath); / / the result is false Path path3 = Paths.get ("c:\ home\\ joe\\ foo"); Path absolutePath3 = path3.toAbsolutePath (); System.out.println (path3 = = absolutePath3); / / the result is true
Similarly, the toAbsolutePath method does not need the file or directory pointed to by Path to exist.
* toRealPath method
This method returns the real path to an existing file or directory (it throws an exception if the file or directory does not exist or cannot be accessed). This method does the following:
If the parameter passed in is true and the file system supports symbolic links, resolve the symbolic links that exist in the path, if any.
If the original Path is a relative path, convert it to an absolute path.
If the path contains redundant information, the redundant information in the returned Path will be removed.
Connect two Path
You can use the resolve method to connect two Path. The parameter to this method is a string. If the string represents a relative path, the path is extended after the original path. If the string passed in is an absolute path, then the value returned is the absolute path passed in. For example:
Java code
Path p1 = Paths.get ("C:\ home\\ joe\ foo"); System.out.format ("% s% n", p1.resolve ("bar")); / / the result is C:\ home\ joe\ foo\ bar Paths.get ("foo"). Resolve ("c:\ home\ joe"); / / result is C:\ home\ joe
Create a path between two paths
This function is somewhat roundabout, but the actual function is to create a relative path between two specified directories or files. For example:
Java code
Path p1 = Paths.get ("joe/foo"); Path p2 = Paths.get ("sally")
In this example, since both paths are relative paths and there is no other information, we will assume that the two joe and sally are sibling directories at the same level, so the following results are obtained
Java code
Path p1_to_p2 = p1.relativize (p2); / / the result is.. /.. / sally Path p2_to_p1 = p2.relativize (p1); / / the result is.. / joe/foo
Let's look at another example:
Java code
Path p1 = Paths.get ("home"); Path p3 = Paths.get ("home/sally/bar"); Path p1_to_p3 = p1.relativize (p3); / / result is sally/bar Path p3_to_p1 = p3.relativize (p1); / / result is.. /.
In this example, the two paths share the same node-home, so the result is not.. / home/sally/bar and.. / home.
If one of the two paths is absolute and the other is relative, the relative method throws an exception. If both paths are absolute, then the behavior of the relative method is related to the system, and different systems may differ.
I experimented with the Windows operating system and found that if the two paths belong to the same hard disk, the execution can be successful, otherwise an exception will be thrown.
Java code
Path path2 = Paths.get ("c:\ abcd\\ efg"); Path path3 = Paths.get ("c:\\ temp"); System.out.println (path2.relativize (path3)); / / the result is..\..\ temp System.out.println (path3.relativize (path2)); / / the result is..\ abcd\ efg Path path4 = Paths.get ("c:\ abcd\\ efg") Path path5 = Paths.get ("d:\\ temp"); System.out.println (path4.relativize (path5)); / / throw an exception
Comparison of Path
Path provides an equals method to check whether two Path are equal. However, it is important to note that the comparison is not whether the two Path points to the same directory or file. Take a look at the following example:
Java code
Path path2 = Paths.get ("abcd\\ 123"); Path path3 = Paths.get ("abcd\\ 123"); Path path4 = Paths.get ("abcd\\.\\ 123"); System.out.println (path2.equals (path3)); / / true System.out.println (path2.equals (path4)); / / false System.out.println (path2.equals (path4.normalize () / / true System.out.println (path2.equals (path2.toAbsolutePath (); / / false
The Path class also provides startsWith and endsWith methods, which are used to check whether the path begins or ends with a specified string, for example:
Java code
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 class implements the Iterable interface, and the iterator method returns an Iterator object, and the * elements in the object are the directory at the top of the original path (closest to the root node). Here is an example of using this method:
Java code
Path path =...; for (Path name: path) {System.out.println (name);}
The Path class also implements the Comparable interface, so you can use compareTo to compare two Path. The algorithms and results of the comparison are related to the provider of the file system and the system platform. Please try it before you use it.
The Path class also provides a method isSameFile to check whether two Path points to the same directory or file. If the Path as a parameter is null, then false is returned directly without checking whether the file pointed to by Path exists. If the two Path are from different file system providers, the false will also be returned directly without checking whether the file or directory exists. If the return result of the equals method executed by two Path is true, then this method directly returns true without checking whether the file or directory exists. In other cases, whether or not to open or access the file or directory pointed to by Path is related to the specific implementation, that is, different JDK/JRE may have different behaviors.
Verify that the file or directory exists
Many of the methods described above do not verify that the file or directory pointed to by Path exists, but simply manipulate the Path instance itself. But in some cases where you need to access the file system to verify the existence of files and directories, you can use the exists and notExists methods. Note that! path.exists () is not equal to path.notExists (). When you call these two methods, there are three situations:
* File or directory is confirmed to exist
* the file or directory was confirmed not to exist
* do not know whether the file or directory exists. This happens when the program does not have access to this file or directory.
If both exists () and notExists () return false, the existence of the file cannot be verified.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.