Share on Google+Share on Google+

Java file get name

Advertisement
In this section, you will learn how to get the name of the file.

Java file get name

In this section, you will learn how to get the name of the file.

Description of code:

You can see in the given example, we have created an object of File class and specified a file in the constructor. Then we have called the method getName() through the object of File class which returns the name of the file.

Here is the code:

import java.io.*;

public class FileGetName {
	public static void main(String[] args) {
		File file = new File("C:/Answers/File/out.txt");
		String st = file.getName();
		System.out.println("File name is: " + st);
	}
}

Through the method getName() of File class, you can get the name of any file.

Output:

File name is: out.txt

Advertisements

Posted on: April 17, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.