Java file get name


 

Java file get name

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

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

Ads