在Java中获取文件名可以使用File类的getName()方法。示例代码如下:
import java.io.File;
public class GetFileNameExample {
public static void main(String[] args) {
File file = new File("C:/example.txt");
String fileName = file.getName();
System.out.println("File name: " + fileName);
}
}
在这个例子中,我们创建了一个File对象,并传入文件路径作为参数。然后调用file.getName()方法来获取文件名,并将结果打印出来。这样就可以获取到文件名了。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:java如何获取file文件名