import java.net.URL; public class URLDemo { public static void main(String[] args) throws Exception { URL url = new URL("https://www.example.com/index.html"); String filename = url.getFile(); System.out.println("Filename: " + filename); } }
Filename: /index.html
import java.net.URL; public class URLDemo { public static void main(String[] args) throws Exception { URL url = new URL("file:///C:/Users/user/Documents/text.txt"); String filename = url.getFile(); System.out.println("Filename: " + filename); } }
Filename: /C:/Users/user/Documents/text.txtThe package library used in these examples is java.net.