Java Utililty Methods URI to File Name

List of utility methods to do URI to File Name

Description

The list of methods to do URI to File Name are organized into topic(s).

Method

StringgetFileName(URI path)
get File Name
return getFileName(path.normalize().getPath());
StringgetFileName(URI uri)
get File Name
String path[] = uri.toString().split("/");
return path[path.length - 1];
StringgetFileName(URI uri)
Extract the file-name portion of a URI.
String path = uri.toString();
int lastSlashIndex = path.lastIndexOf("/");
return path.substring(lastSlashIndex + 1, path.length());
StringgetFileName(URI uri)
get File Name
if (uri == null) {
    return null;
String[] splits = uri.getPath().split(File.separator);
return splits[splits.length - 1];
StringgetFileName(URI uri)
get File Name
if (uri == null) {
    return null;
String uriPath = uri.getPath();
IPath path = Path.fromPortableString(uriPath);
if (path == null) {
    return null;
return path.lastSegment();
StringgetFileNameFromURI(final URI resourceAddress, final boolean usePathStyleUris)
Gets the file name from the URI.
final String[] pathSegments = resourceAddress.getRawPath().split("/");
final int shareIndex = usePathStyleUris ? 2 : 1;
if (pathSegments.length - 1 <= shareIndex) {
    throw new IllegalArgumentException(String.format("Invalid file address '%s'.", resourceAddress));
} else {
    return pathSegments[pathSegments.length - 1];
StringgetFilenameFromURI(URI uri, boolean preserveExtension)
get Filename From URI
int slashIndex = uri.toString().lastIndexOf('/');
int dotIndex = uri.toString().lastIndexOf('.');
String filenameWithoutExtension = null;
if (dotIndex == -1 || preserveExtension) {
    filenameWithoutExtension = uri.toString().substring(slashIndex + 1);
} else {
    filenameWithoutExtension = uri.toString().substring(slashIndex + 1, dotIndex);
return filenameWithoutExtension;
StringgetFilePart(String uri)
get File Part
try {
    URI u = URI.create(uri);
    uri = u.getPath();
} catch (Exception e) {
    return "http://invalid/uri/";
int begin = uri.lastIndexOf("/");
int end = uri.lastIndexOf(".");
...
URIgetFileSystemURI(URI uri)
Returns the filesystem URI.
if (uri == null) {
    return uri;
try {
    return new URI(uri.getScheme(), uri.getAuthority(), null, null, null);
} catch (URISyntaxException x) {
    throw new IllegalArgumentException(x.getMessage(), x);