Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does abstract path means in java.io?

Tags:

java

file

path

io

In java doc about

File#getPath()

writes:

 Converts this abstract pathname into a pathname string.

I try to write1

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

In cmd I see D:\\work

I try to write2:

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

In cmd I see:

file4

Thus I have a question:

What the difference between

abstract pathname

and

pathname string

?

like image 428
gstackoverflow Avatar asked Jul 07 '14 12:07

gstackoverflow


People also ask

What is abstract path in Java?

An abstract pathname has two components: An optional system-dependent prefix string, such as a disk-drive specifier, "/" for the UNIX root directory, or "\\" for a Microsoft Windows UNC pathname, and. A sequence of zero or more string names.

What represents this abstract pathname?

An abstract pathname consists of an optional prefix string, such as disk drive specifiers, “/” for Unix, or “\\” for Windows, and a sequence of zero or more string names. The prefix string is platform-dependent. The last name in the abstract pathname represents a file or directory.

What does path mean in Java?

A Path can represent a root, a root and a sequence of names, or simply one or more name elements. A Path is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system.


2 Answers

An abstract pathname is a java.io.File object and a pathname string is a java.lang.String object. Both reference the same file on the disk.

How do I know?

The first sentence of the Javadoc of java.io.File explains:

An abstract representation of file and directory pathnames.

It goes on to explain why:

User interfaces and operating systems use system-dependent pathname strings to name files and directories. This class presents an abstract, system-independent view of hierarchical pathnames.

like image 192
Erwin Bolwidt Avatar answered Oct 21 '22 05:10

Erwin Bolwidt


The abstract pathname is just the string form of the file/location held in the File object.

If you check the javadoc of File#toString():

Returns the pathname string of this abstract pathname. This is just the string returned by the getPath() method.

like image 39
icza Avatar answered Oct 21 '22 05:10

icza