Here you can find the source of resourceExists(String s)
Parameter | Description |
---|---|
s | a parameter |
public static boolean resourceExists(String s)
//package com.java2s; /**/* w w w .j ava2 s . co m*/ * Copyright 2011 The ARIES Consortium (http://www.ariesonline.org) and * www.integratedmodelling.org. This file is part of Thinklab. Thinklab 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. Thinklab 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 Thinklab. If not, see <http://www.gnu.org/licenses/>. */ import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; public class Main { /** * Resource indicated by s can be a file name or a URL. Returns true if it's an * existing file or a URL that can be opened. * * @param s * @return */ public static boolean resourceExists(String s) { boolean ret = false; Object o = getSourceForResource(s); if (o != null) { if (o instanceof File) { ret = ((File) o).exists(); } else if (o instanceof URL) { try { ret = ((URL) o).openStream() != null; } catch (IOException e) { } } } return ret; } /** * Analyze the passed string and determine if it specifies an existing file resource * or URL. Return the appropriate object, that must be disambiguated using instanceof. * Meant to (inelegantly) solve problems coming from file name encodings in primitive * OS (e.g. Windows) that cannot be handled properly in file:// URLs. * @return a valid URL, existing file, or null. No exceptions are thrown. */ public static Object getSourceForResource(String s) { File f = new File(s); if (f.exists()) return f; URL url = null; try { url = new URL(s); } catch (MalformedURLException e) { } if (url != null) return url; return null; } }