java中通過檔案路徑獲取檔名
阿新 • • 發佈:2019-02-03
- import java.io.File;
- publicclass Test
- {
- publicstaticvoid 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
- // 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);
- }
- }
結果:
-
//1 ---> fileName.txt
- //2 ---> fileName.txt