Java File Path, Absolute Path and Canonical Path

Filed Under: Java

Today we will look into the Java file path. Java File path can be abstract, absolute or canonical.

Java File Path contains three methods for determining the file path, we will explore them in this tutorial.

  1. getPath(): This file path method returns the abstract pathname as String. If String pathname is used to create File object, it simply returns the pathname argument. If URI is used as argument then it removes the protocol and returns the file name.
  2. getAbsolutePath(): This file path method returns the absolute path of the file. If File is created with absolute pathname, it simply returns the pathname.

    If the file object is created using a relative path, the absolute pathname is resolved in a system-dependent way. On UNIX systems, a relative pathname is made absolute by resolving it against the current user directory.

    On Microsoft Windows systems, a relative pathname is made absolute by resolving it against the current directory of the drive named by the pathname, if any; if not, it is resolved against the current user directory.

  3. getCanonicalPath(): This path method returns the canonical pathname that is both absolute and unique. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath method, and then maps it to its unique form in a system-dependent way.

    This typically involves removing redundant names such as “.” and “..” from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

Java File Path Example

Let’s see different cases of the file path in java with a simple program.

package com.journaldev.files;


public class JavaFilePath {

	public static void main(String[] args) throws IOException, URISyntaxException {
		File file = new File("/Users/pankaj/test.txt");
		// relative path
		file = new File("test.xsd");
		// complex relative paths
		file = new File("/Users/pankaj/../pankaj/test.txt");
		// URI paths
		file = new File(new URI("file:///Users/pankaj/test.txt"));

	private static void printPaths(File file) throws IOException {
		System.out.println("Absolute Path: " + file.getAbsolutePath());
		System.out.println("Canonical Path: " + file.getCanonicalPath());
		System.out.println("Path: " + file.getPath());


Below image shows the output produced by the above java file path program.

java file path, file path in java, absolute path, canonical path

The output is self-explanatory. Based on the output, using the canonical path is best suitable to avoid any issues because of relative paths.

Also, note that the java file path methods don’t check if the file exists or not. They just work on the pathname of the file used while creating the File object. That’s all for different types of the file path in java.

You can checkout more java IO examples from our GitHub Repository.


  1. Arun says:

    Hi Sir,

    I am trying to get the full path of the selected file.

    Below is the code. Please advise. I need to pass the full path and file name for the selected files to apache pdf box class to get the excel extracts.

    * To change this license header, choose License Headers in Project Properties.
    * To change this template file, choose Tools | Templates
    * and open the template in the editor.
    package com.pdfet.pdfext;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.event.ActionListener;
    //import com.pdfet.pdfext.ExtensionFileFilter;
    import javax.swing.filechooser.FileFilter;
    * @author vak_salem
    class ExtensionFileFilter extends FileFilter
    String descrip;
    String extn[];

    public ExtensionFileFilter(String descrip,String Extn)
    this(descrip,new String[] {Extn});
    public ExtensionFileFilter(String descrip, String extn[])
    if (descrip==null)
    this.descrip=extn[0] + “{“+ extn.length + “}”;
    this.extn=(String []) extn.clone();

    private void toLower(String array[])
    for (int i=0,n=array.length;i<n;i++)
    // return (array.toString());
    public String getDescription() {
    return descrip;

    public boolean accept(File file) {
    if (file.isDirectory()) {
    return true;
    } else {
    String path = file.getAbsolutePath().toLowerCase();
    for (int i = 0, n = extn.length; i < n; i++) {
    String ex = extn[i];
    if ((path.endsWith(ex) && (path.charAt(path.length() – ex.length() – 1)) == '.')) {
    return true;
    return false;

    public class Jcustomfhooser{

    public static void main(String args[])
    JFrame frame=new JFrame("PDF2Excel");
    frame.setSize(700, 700);
    //JPanel panel=new JPanel();
    JLabel label=new JLabel("Upload file and click Submit");
    Container pane= frame.getContentPane();

    JFileChooser jfc =new JFileChooser("UploadFile");
    // FileFilter pdffilter=new ExtensionFileFilter(null,new String[] {"PDF"});
    //ExtUI ex=new ExtUI();
    //String outstr;
    //ExtensionFileFilter ex=new ExtensionFileFilter();
    FileFilter filter= new ExtensionFileFilter(null,new String[] {"pdf"});
    // FileFilter pdffilter=new ExtensionFileFilter
    JButton button=new JButton("Select Files..");
    button.addActionListener(new ActionListener()
    public void actionPerformed(ActionEvent e)
    int retval=jfc.showOpenDialog(frame);
    if (retval==JFileChooser.APPROVE_OPTION) {
    //File file=new File();
    File[] selectedfiles =jfc.getSelectedFiles();
    StringBuilder sb=new StringBuilder();
    for (int i=0;i<selectedfiles.length;i++)

    sb.append( selectedfiles[i].getName()); // selectedfiles[i]. getName()+ "\n"); }
    JOptionPane.showMessageDialog(frame, sb.toString());

    pane.setLayout(new GridLayout(3,1,10,10));

    frame.setSize(200, 200);


  2. Damien Brock says:

    Try to use Long Path Tool, it really can help you with that.

  3. Tristan says:

    Does getAbsolutePath has to resolve symbolic links? According to this article resolving symbolic links (which always is system dependent) does only happen when calling getCanonicalPath, but a simple test case shows that getAbsolutePath also resolves symbolic links. Do you have more information about this?

  4. Mayuri says:

    This will work bt i want external USB device path then what should we do??

    1. Pankaj says:

      When you connect the external USB drive, it gets mounted and you can use the same path for accessing files in it.

Comments are closed.

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors