Java file dirname and basename 

Joined:
02/21/2009
Posts:
130

July 06, 2010 19:35:00    Last update: July 06, 2010 19:35:00
Java has built-in functions to get the basename and dirname for a given file path, but the function names are not so self-apparent.

import java.io.File;

public class JavaFileDirNameBaseName {
    public static void main(String[] args) {
	File theFile = new File("../foo/bar/baz.txt");
	System.out.println("Dirname: " + theFile.getParent());
	System.out.println("Basename: " + theFile.getName());
    }
}


Results:
C:\tmp>java JavaFileDirNameBaseName
Dirname: ..\foo\bar
Basename: baz.txt
Share |
| Comment  | Tags