If you work strictly with file paths, try this
String path = "C:\\Users\\Ewen\\AppData\\Roaming\\MyProgram\\Test.txt"; File f = new File(path); System.out.println(f.getName());
Thanks, but I also want to remove .txt
Ok try this
String fName = f.getName(); System.out.println(fName.substring(0, fName.lastIndexOf('.')));
See more details.
source share