As I understand it, file.getAbsolutePath()
gets the full path for a file. I thought file.getAbsoluteFile()
would just get the file name, and not the path, but that didn't work. Basically I want to display the files and directories in a JTree
, but for obvious reasons, I don't want the path. Just the name. How can I get this done?
Asked
Active
Viewed 649 times
0

user10670371
- 11
-
Check the answer by Duncan Jones in [this duplicate](https://stackoverflow.com/questions/26019132/how-to-split-a-file-path-to-get-the-file-name) – Hovercraft Full Of Eels Nov 18 '18 at 13:00
-
Also please use a [basic Google search strategy of this site](https://www.google.com/search?q=java+how+to+get+the+basic+file+name+site:stackoverflow.com) (please check link) before asking a likely duplicate question. – Hovercraft Full Of Eels Nov 18 '18 at 13:01
-
1@HovercraftFullOfEels (and/or read the Javadoc of the class you're using) – Andy Turner Nov 18 '18 at 13:06
-
@AndyTurner: indeed! :) – Hovercraft Full Of Eels Nov 18 '18 at 13:07
1 Answers
1
Use file.getName()
:
Returns the name of the file or directory denoted by this abstract pathname. This is just the last name in the pathname's name sequence.

Andy Turner
- 137,514
- 11
- 162
- 243