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
return new File(Uri.parse(url).getPath()).getName()
In order to return filename without extension and without parameters use the following:
String filenameWithParams = FilenameUtils.getBaseName(urlStr); // may hold params if http://example.com/a?param=yes
return filenameWithParams.split("\\?")[0]; // removing parameters from url if they exist
In order to return filename with extension without params use this:
/** Parses a URL and extracts the filename from it or returns an empty string (if filename is non existent in the url) <br/>
* This method will work in win/unix formats, will work with mixed case of slashes (forward and backward) <br/>
* This method will remove parameters after the extension
*
* @param urlStr original url string from which we will extract the filename
* @return filename from the url if it exists, or an empty string in all other cases */
private String getFileNameFromUrl(String urlStr) {
String baseName = FilenameUtils.getBaseName(urlStr);
String extension = FilenameUtils.getExtension(urlStr);
try {
extension = extension.split("\\?")[0]; // removing parameters from url if they exist
return baseName.isEmpty() ? "" : baseName + "." + extension;
} catch (NullPointerException npe) {
return "";
}
}
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<String> 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);
}
}
Instead of reinventing the wheel, how about using Apache commons-io:
import org.apache.commons.io.FilenameUtils;
public class FilenameUtilTest {
public static void main(String[] args) throws Exception {
URL url = new URL("http://www.example.com/some/path/to/a/file.xml?foo=bar#test");
System.out.println(FilenameUtils.getBaseName(url.getPath())); // -> file
System.out.println(FilenameUtils.getExtension(url.getPath())); // -> xml
System.out.println(FilenameUtils.getName(url.getPath())); // -> file.xml
}
}
Get File Name with Extension, without Extension, only Extension with just 3 line:
String urlStr = "http://www.example.com/yourpath/foler/test.png";
String fileName = urlStr.substring(urlStr.lastIndexOf('/')+1, urlStr.length());
String fileNameWithoutExtension = fileName.substring(0, fileName.lastIndexOf('.'));
String fileExtension = urlStr.substring(urlStr.lastIndexOf("."));
Log.i("File Name", fileName);
Log.i("File Name Without Extension", fileNameWithoutExtension);
Log.i("File Extension", fileExtension);
Log Result:
File Name(13656): test.png
File Name Without Extension(13656): test
File Extension(13656): .png
Hope it will help you.
I've come up with this:
String url = "http://www.example.com/some/path/to/a/file.xml";
String file = url.substring(url.lastIndexOf('/')+1, url.lastIndexOf('.'));