Getting an Absolute Filename Path from a Relative Filename Path


File file = new File("somefile.txt"); File absPath = file.getAbsoluteFile();



In this phrase, we get an absolute path for a file for which we specify a relative path. An absolute pathname gives the full path for a file starting from the file system's root directory, i.e. c:\project\book\somefile.txt. A relative pathname specifies a file's name and path relative to the current directory, i.e. somefile.txt if the current directory is c:\project\book. In many cases, a relative pathname consists of only the filename. The getAbsoluteFile() method of the File class returns a File object representing the absolute filename for the file represented by the File object on which it is called.

There is a similar method called getAbsolutePath() that returns the absolute path as a String instead of as a File object. The code below shows this method:

File file = new File("filename.txt"); String absPath = file.getAbsolutePath();


In this example, absPath would contain the string "c:\project\book\somefile.txt".




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net