Lessons Learn JavaIO: File and Path Path

brief introduction

What does a file have to do with a path?What secrets do files and paths hide?What are the ways to create paths under the management of the file system?Today, Brother F brought his little sister and brother to give you another wonderful performance.

Files and Paths

Little sister: Brother F, I have a problem. File in java is a class that can be understood because it contains a lot of other information, but why does Path need to be a separate class?Isn't it easier to just use one String?

More interesting and see:

More please visit www.flydean.com

Everything has a cause. 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, File is a class that represents the properties and functions that all file systems have. Whether you are windows or linux, the File objects in them should be the same.

File contains Path. Look, Path is an interface. Why is it an interface?Because Path can be divided into JrtPath, UnixPath, and ZipPath depending on the situation.FileSystem corresponding to the three Paths was discussed in the previous article.So Path implementations are different, but Files containing Path are the same.

Little sister: Brother F, how can this be so cruel? Give me a plain and popular explanation.

In that case, let me explain: patriotic version, maybe we belong to different nationalities, but we are all Chinese.Popular version, we are all cultural people, so why do you do this?Culture edition, in the same nine years, how do you show?

Looking at their implementation interfaces, File implements a Serializable representation that can be serialized, a Comparable representation that can be sorted.

Path inherits Comparable, meaning it can be sorted.Inherited Iterable s indicate that they can be traversed because Path can represent directories.Inheriting Watchable means that it can be registered with WatchService for monitoring.

Different paths in the file

Little sister: Brother F, File has several get methods for Path. Can you tell us the difference?

Code directly:

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, getPath, getAbsolutePath, and getCanonicalPath.

The result returned by getPath is the path that was passed in when the new File was created. Enter what to return and what to return.

getAbsolutePath returns an absolute path, which is to precede the getPath with the current path.

getCanonicalPath returns a simplified AbsolutePath, or a designatory symbol such as.Or..

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 a different Path

Little sister: Brother F, I remember that paths have relative paths, absolute paths, etc. Is there a corresponding way to create Path?

Of course, first look at the creation of absolute paths:

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 passes in the address of the absolute path to construct the absolute path.

Use the same Paths.get Method, incoming non-absolute paths can construct relative paths.

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

We can also build Path from URI s:

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());

Path can also be built from FileSystem:

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

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



There are many ways to create Path, there is always one that works for you.Let's pick one.

Examples of this article https://github.com/ddean2009/learn-java-io-nio

Author: Fldean Programs

Links to this article: http://www.flydean.com/java-io-file-path/

Source: Fldean's blog

Welcome to my Public Number: program stuff, more exciting waiting for you!

Tags: Programming Java Spring Windows Linux

Posted on Mon, 08 Jun 2020 18:51:23 -0400 by jonabomer