Java program to get the name of the file from the absolute path
httwww//:sp.theitroad.com
To get the name of the file from an absolute path in Java, you can use the File
class provided by Java. The File
class has a method called getName()
that returns the name of the file represented by the File
object. Here is an example program that demonstrates how to get the file name from an absolute path:
import java.io.File; public class GetFileNameFromAbsolutePath { public static void main(String[] args) { String absolutePath = "C:\\Users\\Username\\Documents\\example.txt"; File file = new File(absolutePath); String fileName = file.getName(); System.out.println("File name: " + fileName); } }
In this program, we define an absolute path to a file as a String
, and then create a File
object from the path. We then use the getName()
method to get the name of the file represented by the File
object. The program then prints out the file name to the console.
When you run this program, you should get output like this:
File name: example.txt
This shows that the file name is correctly extracted from the absolute path.