java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to get the absolute path of a file?

package com.javacoderanch.sample.java.io;

import java.io.File;

public class AbsolutePathExample
{
    public static void main(String[] args)
    {
        // Create an instance of File called file.
        File file = new File("JavaProgrammingBook.pdf");

        // Now we want to know where is exactly this file is
        // located in our file system. To do this we can use
        // the getAbsolutePath() method of the File class.
        String absolutePath = file.getAbsolutePath();

        // Print out the JavaProgrammingBook.pdf location to
        // the console.
        System.out.println("AbsolutePath = " + absolutePath);
    }
}

Here is the result of the program:

AbsolutePath = D:DataProjects2005kodejava-samplesJavaProgrammingBook.pdf