Programming Tips - Java: Get the full (absolute) and basename of a File object

Date: 2012aug1 Update: 2025sep23 Language: Java Level: novice Q. Java: Get the full (absolute) and basename of a File object A. Show in this full example:
import java.io.File; class Demo { public static final void main(String []args) { File file = new File("/tmp/hello.txt"); String full = file.getAbsolutePath(); // HERE String basename = file.getName(); // HERE System.out.println("full=" + full); System.out.println("basename=" + basename); } }
Output:
full=/tmp/hello.txt basename=hello.txt
Notice that the File object doesn't need to exist.