In Java, given a java.net.URL or a String in the form of http://www.example.com/some/path/to/a/file.xml , what is the easiest way to g
Beyond the all advanced methods, my simple trick is StringTokenizer:
import java.util.ArrayList;
import java.util.StringTokenizer;
public class URLName {
public static void main(String args[]){
String url = "http://www.example.com/some/path/to/a/file.xml";
StringTokenizer tokens = new StringTokenizer(url, "/");
ArrayList parts = new ArrayList<>();
while(tokens.hasMoreTokens()){
parts.add(tokens.nextToken());
}
String file = parts.get(parts.size() -1);
int dot = file.indexOf(".");
String fileName = file.substring(0, dot);
System.out.println(fileName);
}
}