Java URL to File Name getFile(URL url)

Here you can find the source of getFile(URL url)

Description

Resolves a File from URL .

License

Open Source License

Parameter

Parameter Description
url the URL to resolve to File

Return

File represented by the URL, or null if the URL is not file-based.

Declaration

public static File getFile(URL url) 

Method Source Code


//package com.java2s;
/*/*from   ww  w .j  a  v a  2  s .  c  o  m*/
  Copyright 2012  Andrius Velykis
      
  This file is part of the CZT project.
    
  The CZT project is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
    
  The CZT project is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with CZT.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.File;
import java.net.URL;

public class Main {
    /**
     * Resolves a {@link File} from {@link URL}. Checks if the protocol is `file:`, otherwise returns
     * {@code null}.
     * 
     * @param url  the URL to resolve to File
     * @return File represented by the URL, or {@code null} if the URL is not file-based.
     */
    public static File getFile(URL url) {

        if (url == null) {
            return null;
        }

        if ("file".equals(url.getProtocol())) {
            return new File(url.getFile());
        }

        // for non-files, just return null
        return null;
    }
}

Related

  1. getFile(URL resourceUrl, String description)
  2. getFile(URL ressource)
  3. getFile(URL url)
  4. getFile(URL url)
  5. getFile(URL url)
  6. getFile(URL url)
  7. getFile(URL url, String description)
  8. getFileAbsoluteURL(String[] directoryPaths, String fileName)
  9. getFileDateTime(URL url)