我尝试使用 Path 接口;
//get a path object with relative path
Path filePath = Paths.get("C:\\Test\\filename.txt");
System.out.println("The file name is: " + filePath.getFileName());
Path filePath2 = Paths.get("/home/shibu/Desktop/filename.txt");
System.out.println("The file name is: " + filePath2.getFileName());
输出就像;
The file name is: C:\Test\filename.txt
The file name is: filename.txt
对于 windows 文件,它打印完整路径,对于 linux 文件,它只打印文件名。
为什么会有这种差异?