What does the abstract path mean in java.io?

In java doc about

File#getPath() 

writes:

  Converts this abstract pathname into a pathname string. 

I'm trying to write 1

 File file3 = new File("D:\\work"); System.out.println(file3.getPath()); 

In cmd, I see D:\\work

I am trying to write down 2:

 File file4= new File("file4"); System.out.println(file4.getPath()); 

In cmd, I see:

 file4 

So I have a question:

What's the difference between

abstract path

and

path name string

?

+6
source share
3 answers

The abstract path is the java.io.File object, and the path string is the java.lang.String object. Both refer to the same file on disk.

How do I know?

The first Javadoc sentence from java.io.File explains:

Abstract representation of file and directory names.

The following explains why:

User interfaces and operating systems use system-dependent pathnames to specify files and directories. This class represents an abstract, system-independent view of hierarchical routes.

+6
source

An abstract path is just the string form of the file / location contained in the File object.

If you check javadoc File#toString() :

Returns the path string of this abstract path. This is only the string returned by the getPath() method.

+1
source

See javadoc: abstract pathname = File

  • An optional system prefix string, such as a disk specifier, "/" for the UNIX root directory or "\\" for the Microsoft Windows UNC path name and
  • A sequence of null or more string names. [referring to directories and file

They do not depend on the features of the operating system notation.

The string form gives you what you need to write to the current operating system in order to link to this file.

+1
source

Source: https://habr.com/ru/post/971866/


All Articles