EN
Java - how to get file name from absolute path string?
1 answers
0 points
Can you tell me how to get the file name from the absolute path string in Java?
I created a file as follows:
xxxxxxxxxx
1
String myDirectoryPath = "C:\\projects\\example\\myFile.txt";
xxxxxxxxxx
1
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
I found the solution.
Instead of:
xxxxxxxxxx
1
System.out.println(myDirectory);
use getName() method:
xxxxxxxxxx
1
System.out.println(myDirectory.getName());
0 commentsShow commentsAdd comment