Java Utililty Methods URL to File Name

List of utility methods to do URL to File Name

Description

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

Method

FilegetFile(URL url)
Resolves a File from URL .
if (url == null) {
    return null;
if ("file".equals(url.getProtocol())) {
    return new File(url.getFile());
return null;
FilegetFile(URL url)
get File
if (url != null) {
    if ("file".equalsIgnoreCase(url.getProtocol())) {
        return new File(url.toString().substring(5));
return null;
FilegetFile(URL url, String description)
get File
if (!URL_PROTOCOL_FILE.equals(url.getProtocol())) {
    throw new FileNotFoundException(description + " cannot be resolved to absolute file path "
            + "because it does not reside in the file system: " + url);
try {
    return new File(url.toURI().getSchemeSpecificPart());
} catch (URISyntaxException ex) {
    return new File(url.getFile());
...
StringgetFileAbsoluteURL(String[] directoryPaths, String fileName)
get File Absolute URL
File fileToReturn = null;
String fullPathNameToReturn = null;
int numFound = 0;
File file = new File(fileName);
if (file.isAbsolute() && file.exists() && file.isFile() && file.canRead()) {
    numFound = 1;
    fileToReturn = file;
    try {
...
longgetFileDateTime(URL url)
Gets file date and time.
if (url == null) {
    return -1;
String fileName = url.getFile();
if (fileName.charAt(0) == '/' || fileName.charAt(0) == '\\') {
    fileName = fileName.substring(1, fileName.length());
try {
...
StringgetFileExtensionByURL(URL url)
Returns the file extension from the file part of the URL.
String trimFile = url.getFile().trim();
if (trimFile == null || trimFile == "" || trimFile == "/") {
    return null;
int strIndex = trimFile.lastIndexOf("/");
String filePart = trimFile.substring(strIndex + 1, trimFile.length());
strIndex = filePart.lastIndexOf(".");
if (strIndex == -1 || strIndex == filePart.length() - 1) {
...
FilegetFileFor(URL url1)
get File For
if (urlFileMap == null)
    urlFileMap = new HashMap();
if (urlFileMap.get(url1) != null)
    return (File) urlFileMap.get(url1);
File newFile = new File(url1.getFile());
urlFileMap.put(url1, newFile);
return newFile;
FilegetFileForURL(String url)
get File For URL
if (!url.startsWith("file:/")) {
    url = "file:/" + url;
url = url.replaceAll("%20", " ");
try {
    return new File((new URL(url)).getFile());
} catch (MalformedURLException e) {
    throw new RuntimeException(e);
...
FilegetFileForUrl(URL url)
Return the File referenced by a "file:" or "jar:file:" URL.
return (url == null ? null : getFileForUrl(url.toString()));
FilegetFileFrom(final URL url)
get File From
if (url != null) {
    final String resourceAsString = url.toExternalForm();
    if (resourceAsString.startsWith("file:/")) {
        final int startIndex = isWindows() ? 6 : 5;
        final String fileName = resourceAsString.substring(startIndex);
        final File file = new File(fileName);
        return file;
return null;