Introduction to the

What is the relationship between a file and a path? What secrets do files and paths hide? What are the ways to create a path under the management of a file system? Today, brother F and his younger sister will give you another wonderful performance.

Files and Paths

F: I have a question. It is understandable that the File File in Java is a class, because the File contains a lot of other information, but why should the Path be a separate class? Wouldn’t it be easier to just use a String?

More highlights:

  • Blockchain from getting started to Giving up series tutorials – with ongoing updates covering cryptography, Hyperledger, Ethereum,Libra, Bitcoin and more
  • Spring Boot 2.X Series tutorials: Learn Spring Boot from Scratch in seven days – continuous updates
  • Spring 5.X Series tutorials: Everything you can think of in Spring5 – constantly updated
  • Java Programmer from Handyman to Expert to God (2020 edition) – Ongoing updates with detailed articles and tutorials

For more, visit www.flydean.com

Everything happens for a reason. There is no such thing as love or hatred without any reason or cause. It’s all so wonderful.

Let’s look at the definitions of File and path:

public class File
   implements Serializable.Comparable<File>
Copy the code
public interface Path
    extends Comparable<Path>, 可迭代<Path>, Watchable
Copy the code

First, File is a class that represents properties and functionality that all File systems have, and File objects in them should be the same whether you’re Windows or Linux.

“Path” is an interface. “Path” is an interface. Because Path can be divided into JrtPath, UnixPath, and ZipPath depending on the situation. The FileSystem corresponding to the three paths was discussed in the previous article. So the implementation of Path is different, but the File containing Path is the same.

Brother F, why is this so difficult to say? Please give me a straightforward and popular explanation.

In that case, let me explain: patriotic version, we may belong to different nationalities, but we are all Chinese. Popular version, we are all cultural people, why you so drag. Cultural edition, same year, ru He xiu?

File implements Serializable, which can be serialized, and implements Comparable, which can be sorted.

Path inherits Comparable, which means it can be sorted. Inheriting Iterable means Iterable, Iterable because Path represents a directory. Inheriting Watchable means that it can be registered with the WatchService for monitoring.

Different paths in the file

F: There are several get methods about Path in File. Can you tell us the differences between them?

Directly on the code:

public void getFilePath(a) throws IOException {
        File file= new File(".. /.. /www.flydean.com.txt");
        log.info("name is : {}",file.getName());

        log.info("path is : {}",file.getPath());
        log.info("absolutePath is : {}",file.getAbsolutePath());
        log.info("canonicalPath is : {}",file.getCanonicalPath());
    }
Copy the code

There are three methods related to Path in File: getPath, getAbsolutePath, and getCanonicalPath.

GetPath returns the same path that was passed to new File.

GetAbsolutePath returns the absolute path by adding the current path to getPath.

GetCanonicalPath returns a simplified AbsolutePath that has been removed. Or.. And so on.

Take a look at the output:

INFO com.flydean.FilePathUsage - name is : www.flydean.com.txt INFO com.flydean.FilePathUsage - path is : .. /.. /www.flydean.com.txt INFO com.flydean.FilePathUsage - absolutePath is : /Users/flydean/learn-java-io-nio/file-path/.. /.. /www.flydean.com.txt INFO com.flydean.FilePathUsage - canonicalPath is : /Users/flydean/www.flydean.com.txtCopy the code

Build different paths

Brother F, I remember there are relative paths, absolute paths, etc. Is there a corresponding way to create a Path?

Of course there is, let’s look at creating an absolute path:

public void getAbsolutePath(a){
        Path absolutePath = Paths.get("/data/flydean/learn-java-io-nio/file-path"."src/resource"."www.flydean.com.txt");
        log.info("absolutePath {}",absolutePath );
    }
Copy the code

We can build absolute Paths using the paths.get method, passing in the address of the absolute path.

Also using the paths.get method, passing in non-absolute Paths builds relative Paths.

public void getRelativePath(a){
        Path RelativePath = Paths.get("src"."resource"."www.flydean.com.txt");
        log.info("absolutePath {}",RelativePath.toAbsolutePath() );
    }
Copy the code

We can also build a Path from a URI:

public void getPathfromURI(a){
        URI uri = URI.create("file:///data/flydean/learn-java-io-nio/file-path/src/resource/www.flydean.com.txt");
        log.info("schema {}",uri.getScheme());
        log.info("default provider absolutePath {}",FileSystems.getDefault().provider().getPath(uri).toAbsolutePath().toString());
    }
Copy the code

You can also build a Path from FileSystem:

public void getPathWithFileSystem(a){
            Path path1 = FileSystems.getDefault().getPath(System.getProperty("user.home"), "flydean"."flydean.txt");
           log.info(path1.toAbsolutePath().toString());

            Path path2 = FileSystems.getDefault().getPath("/Users"."flydean"."flydean.txt");
            log.info(path2.toAbsolutePath().toString());

        }
Copy the code

conclusion

There are so many ways to create a Path, there is always one for you. Come and choose.

Examples of this article github.com/ddean2009/l…

Author: Flydean program stuff

Link to this article: www.flydean.com/java-io-fil…

Source: Flydean’s blog

Welcome to pay attention to my public number: procedures those things, more wonderful waiting for you!