Home Tutorial Java Core Files Java file get name

 
 

Java file get name
Posted on: April 17, 2006 at 12:00 AM
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

Related Tags for Java file get name:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.