Search This Blog

Parsing URL to get the file name

e.g. I want to parse to get the file name, 1.txt, from the url: file:/home/wilson/Desktop/1.txt
public static String getFileName(String url) throws MalformedURLException {
  URL u = new URL(url);
  String path = u.getPath();
  if (path != null) {
   String[] tokens = path.split("/");
   return tokens[tokens.length - 1];
  }
  return null;
 }


See also: Parsing a URL

No comments:

Post a Comment