How to get the file name from a string containing the absolute file path in Java

1 Answer

0 votes
import java.io.File;

public class MyClass {
    public static void main(String args[]) {
        String path = "/path/to/file.txt";
        
        File file = new File(path);
        String fileName = file.getName(); 
 
        System.out.println(file);
        System.out.println(fileName);
    }
}
        
        
        
        
/*
run:

/path/to/file.txt
file.txt
        
*/

 



answered Oct 25, 2023 by avibootz

Related questions

3 answers 287 views
2 answers 222 views
1 answer 182 views
1 answer 206 views
1 answer 181 views
1 answer 406 views
...