Java URI Value Check isFile(URI uri)

Here you can find the source of isFile(URI uri)

Description

If there is a file at the other end of this URI return true.

License

Open Source License

Parameter

Parameter Description
uri The URI to check

Return

true if the URI points at a file

Declaration

public static boolean isFile(URI uri) 

Method Source Code

//package com.java2s;
/**//from   w w  w  .  j a  v a 2 s. co  m
 * Distribution License:
 * JSword is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License, version 2.1 as published by
 * the Free Software Foundation. This program 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 Lesser General Public License for more details.
 *
 * The License is available on the internet at:
 *       http://www.gnu.org/copyleft/lgpl.html
 * or by writing to:
 *      Free Software Foundation, Inc.
 *      59 Temple Place - Suite 330
 *      Boston, MA 02111-1307, USA
 *
 * Copyright: 2005
 *     The copyright to this program is held by it's authors.
 *
 * ID: $Id$
 */

import java.io.File;

import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URI;

import java.net.URL;

public class Main {
    /**
     * Constant for the file: protocol or scheme
     */
    public static final String PROTOCOL_FILE = "file";

    /**
     * If there is a file at the other end of this URI return true.
     * 
     * @param uri
     *            The URI to check
     * @return true if the URI points at a file
     */
    public static boolean isFile(URI uri) {
        if (uri.getScheme().equals(PROTOCOL_FILE)) {
            return new File(uri.getPath()).isFile();
        }

        try {
            // This will throw if the resource does not exist
            uri.toURL().openStream().close();
            return true;
        } catch (IOException ex) {
            // the resource does not exist!
            return false;
        }
    }

    /**
     * Convert an URI to an URL.
     * 
     * @param uri
     *            to convert
     * @return the URL representation of the URI
     */
    public static URL toURL(URI uri) {
        try {
            return uri.toURL();
        } catch (MalformedURLException e) {
            return null;
        }
    }
}

Related

  1. isBase(URI base, URI uri)
  2. isDefaultPort(URI uri)
  3. isDirectory(URI orig)
  4. isExtensionUri(final URI uri)
  5. isFile(final URI uri)
  6. isFile(URI uri)
  7. isFileSystemAvailable(File file, URI topLevelResource)
  8. isFileUri(String uri)
  9. isFileURI(URI uri)