Languages

Java - how to get file name from absolute path string?

0 points
Asked by:
Dirask Community
6300

Can you tell me how to get the file name from the absolute path string in Java?

I created a file as follows:

String myDirectoryPath = "C:\\projects\\example\\myFile.txt";
File myDirectory = new File(myDirectoryPath);

Now when I use System.out.println(myDirectory); I get the absolute path of a file.

How do I only get the myFile.txt as a String?

1 answer
0 points
Answered by:
Dirask Community
6300

I found the solution.

Instead of:

System.out.println(myDirectory);

use getName() method:

System.out.println(myDirectory.getName());
0 comments Add comment
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join