Share this page 

Parse a pathnameTag(s): IO


import java.io.File;

public class ParsePathname {
    
public ParsePathname() { }

public static void main(String[] args) throws Exception {
   File f = new File
      ("c:/program files/adobe/Acrobat 5.0/Reader/acrord32.exe");
   System.out.println("dir : " + f.getParent());
   System.out.println("file : " + f.getName());
   System.out.println("ext : " + getFileExtension(f.getName())); 
   System.out.println("url : " + f.toURL());         
   System.out.println("uri : " + f.toURI()); 
   /*
    output :
    dir : c:\program files\adobe\Acrobat 5.0\Reader
    file : acrord32.exe
    ext : exe
    url : file:/c:/program files/adobe/Acrobat 5.0/Reader/acrord32.exe
    uri : file:/c:/program%20files/adobe/Acrobat%205.0/Reader/acrord32.exe
   */
}

public static String getFileExtension(String f) {
    String ext = "";
    int i = f.lastIndexOf('.');
    if (i > 0 &&  i < f.length() - 1) {
        ext = f.substring(i+1).toLowerCase();
    }
    return ext;
}
}