Home Tutorial Java Core Files Java file basename

 
 

Share on Google+Share on Google+
Java file basename
Posted on: June 17, 2006 at 12:00 AM
Advertisement
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
Advertisement

Related Tags for Java file basename:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 17, 2006

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com