import java.io.File; public class Test { public static void main(String[] args) { // path String path1 = "D:/folder1/folder2/folder3/fileName.txt"; String path2 = "/folder2/folder3/fileName.txt"; // file File file1 = new File(path1); File file2 = new File(path2); // get name of file String fileName1 = file1.getName(); String fileName2 = file2.getName(); // print file name System.out.println("1 ---> " + fileName1); System.out.println("2 ---> " + fileName2); } } 结果: [java] view plain copy //1 ---> fileName.txt //2 ---> fileName.txt