Java file basename


 

Java file basename

In this section, you will learn how to determine the basename of the specified file or directory.

In this section, you will learn how to determine the basename of the specified file or directory.

Java file basename

In this section, you will learn how to determine the basename of the specified file or directory.

Description of code:

The basename is actually the name of the specified file or directory. If  file is a directory, the basename will be the last directory element and if file is a full-path, relative-path, or simple filename, the basename will be the simple file name with extension, without any directory elements. In the given example, we have used built in function getName() of File class to  get the base name for the given file path.

Here is the code:

import java.io.File;

public class JavaFileBasename {
	public static void main(String[] args) {
		File f = new File("C:/new.txt");
		System.out.println("Basename: " + f.getName());
	}
}

Through the above code, you can find the base name of any specified file. The method getName() is used to determine the basename.

Output:

Basename: new.txt

Ads