Junior sister learning java IO: file and path

brief introduction

What is the relationship between file and path? What secrets are hidden in files and paths? Under the management of file system, what are the ways to create paths? Today, senior brother F and younger martial sister will give you another wonderful performance.

File and path

Younger martial sister: elder martial brother F, I have a question. It is understandable that the file in Java is a class, because the file contains a lot of other information, but why should the path be a separate class? Isn't it simpler to use only one string?

More highlights:

Everything has a reason. There is no love or hate for no reason. Everything is really wonderful.

Let's look at the definitions of file and path:

public class File
   implements Serializable,Comparable<File>
public interface Path
    extends Comparable<Path>,Iterable<Path>,Watchable

First of all, file is a class that represents the properties and functions of all file systems. Whether you are windows or Linux, the file objects in them should be the same.

File contains path. Look, junior sister, path is an interface. Why is it an interface? Because path can be divided into jrtpath, unixpath and zippath according to different situations. The file system corresponding to the three paths was discussed in the previous article. Therefore, the implementation of path is different, but the file containing path is the same.

Younger martial sister: Senior brother F, why is this so awkward? Give me a straightforward and popular explanation.

In that case, let me explain: Patriotic version, maybe we belong to different nationalities, but we are all Chinese. Popular version, everyone is a cultural person, why do you drag like this. Cultural edition, in the same nine years, you he Xiu?

Looking at the implementation interfaces of the two, file implements the serializable representation that can be serialized and the comparable representation that can be sorted.

Path inherits comparable, indicating that it can be sorted. Inheritable means that it can be traversed because path can represent a directory. Inheriting Watchable means that it can be registered in watchservice for monitoring.

Different paths in the file

Younger martial sister: Senior brother F, there are several get methods about path in file. Can you tell us their differences?

Direct code:

public void getFilePath() 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());
    }

There are three path related methods in file, namely getpath, getabsolutepath and getcanonicalpath.

The result returned by getpath is the path passed in during new file. What you enter returns what.

Getabsolutepath returns @ R_ 301_ 1919@, the current path is added before getpath.

Getcanonicalpath returns the simplified absolutepath, which is removed Or And so on.

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.txt

Build different paths

Younger martial sister: Senior brother F, I remember there are relative paths, @ r_ 301_ 1919 @ etc., is there a corresponding method to create path?

Of course there are. Take a look at @ r first_ 301_ 1919 @ creation:

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

We can use paths Get method passed in @ r_ 301_ 1919 @ to build @ R_ 301_ 1919@。

Also use paths Get method, pass in non @ r_ 301_ 1919@ you can build relative paths.

public void getRelativePath(){
        Path RelativePath = Paths.get("src","resource",RelativePath.toAbsolutePath() );
    }

We can also build paths from URIs:

public void getPathfromURI(){
        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());
    }

You can also build a path from the filesystem:

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

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

        }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>