java tutorial - How to Get file name and file path in Java - java programming - learn java - java basics - java for beginners



 get file name and file path

Learn java - java tutorial - get file name and file path - java examples - java programs

Get file name and file path :

  • In Java File class a member method getName() returns the name of the file or directory denoted by this abstract path name, or the empty string if this path-name’s name sequence is empty and getPath() method returns the path of a file.

File Path:

  • java.io.File contains three methods for determining the file path
  • getPath(): This method returns the abstract pathname as String. If String pathname is used to create File object, just it returns the pathname argument. If URI is used as argument then it eliminates the protocol and returns the file name.
  • getAbsolutePath(): File is created with absolute pathname, it returns only the pathname.
  • getCanonicalPath(): This method returns the both absolute and unique pathname. This method first converts this pathname to absolute form if required, as if by invoking the getAbsolutePath method, and then maps it to its unique form in a system-dependent way.
  • It involves removing redundant names such as “.” and “..” from the pathname, resolving symbolic links (on UNIX platforms), and then change drive letters to a standard case (on Microsoft Windows platforms).

sample code:

import java.io.File;
public class kaashiv_infotech
{
    public static void main(String[] args)
    {
        File myFile = new File("C:" + File.separator + "jdk" + File.separator, "Kasshiv_File.java");
        System.out.println(myFile.getName());
        System.out.println(myFile.getPath());
 
    }
}

Output:

Kasshiv_File.java
C:/jdk/Kasshiv_File.java

Related Searches to How to Get file name and file path in Java