tags:

views:

92

answers:

5
+2  Q: 

doubt in file name

what is the difference b/w abstract pathname and pathname string. I came across these two when i was reading about string separator

+1  A: 

It depends on the system into which you ran your JVM. For instance, Windows and Linux need different slash separator (forward/backward). It's the separatorChar Value in File Class. The Abstract PathName is independant from the system. This pathname is used internaly by The File Class. @see File constructor doc

Vincent
+1  A: 

What is the difference b/w abstract pathname and pathname string?

An abstract pathname is basically a regular path name represented in a OS-independent way, while a pathname string is simply a (possibly system-dependent) string representing a path name.

The documentation for File elaborates a bit on this:

An abstract pathname has two components:

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

  2. A sequence of zero or more string names.

For example, the abstract version of the pathname string

"/home/aioobe/tmp/test.txt"

consists of these two parts:

  1. A prefix: "/"
  2. A list of string names
    1. "home",
    2. "aioobe",
    3. "tmp",
    4. "test.txt"
aioobe
+1  A: 

Pathname strings are used to name files and directories in various operating systems. They vary from OS to OS. For example, in Linux it is: /home/user/a.java and in Windows it: c:\dev\a.java

So, when it's said that a pathname string is converted to abstract pathname, that means that the pathname string is now independent of the OS.

For example:

File path = File("/home/user/a.java")

Converts the linux dependent file path to a JVM understandable file path (path object in above example), which is called Abstract path name.

I guess you have been reading this, but the best way to understand this is to implement a simple 5line java code and see what options with you get with the File class.

zengr
+1  A: 

An abstract pathname has two components:

  1. 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
  2. A sequence of zero or more string names.

This is how Java internally represents a path to a resources in a OS independent manner.

A path name is the readable representation of that abstraction and is also what you could type into your OS terminal to reach that file, ie.

c:\blah\blah.txt

willcodejavaforfood
+2  A: 

Beware that on *nix back slash (\) is valid character in a file name but not on windows.

Run this on windows and *nix and compare printouts:

File f1 = new File("c:\\somepath\\somefile.txt");
System.out.println(f1.getName()); 
File f2 = new File("c:/somepath/somefile.txt");
System.out.println(f2.getName()); 

On windows both variants prints somefile.txt but on *nix the first variant prints

c:\somepath\somefile.txt

second variant prints somefile.txt

So using / is "safer" to use. (Found this when uploading a file from a windows client to a Solaris server and trying to extract just the file name.)

Kennet
Excellent answer. Thanks kennet!!!
Sumithra