Java Resource Path Get resourceExists(String path)

Here you can find the source of resourceExists(String path)

Description

resource Exists

License

Open Source License

Declaration

public static boolean resourceExists(String path) 

Method Source Code


//package com.java2s;
/*/*from ww w .j ava2  s .co m*/
 * RED: RNA Editing Detector
 *     Copyright (C) <2014>  <Xing Li>
 *
 *     RED 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.
 *
 *     RED 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Main {
    public static boolean resourceExists(String path) {
        if (isRemote(path)) {
            try {
                URL url = createURL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                int code = conn.getResponseCode();
                return code >= 200 && code < 300;
            } catch (IOException e) {
                return false;
            }
        } else {
            return (new File(path)).exists();
        }
    }

    public static boolean isRemote(String path) {
        return path != null
                && (path.startsWith("http://") || path.startsWith("https://") || path.startsWith("ftp://"));
    }

    public static URL createURL(String url) {
        try {
            return new URL(url);
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related

  1. getStringFromResource(Class clazz, String path)
  2. getSystemResource(String path)
  3. getSystemResource(String path)
  4. loadResource(final Class bundleClazz, final Class resourceTypeclazz, final String pathToFile)
  5. loadResources(ClassLoader loader, String path)