Here you can find the source of getInputStreamForResource(String s)
public static InputStream getInputStreamForResource(String s)
//package com.java2s; /**/*w w w. j a v a 2s .c o 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.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; public class Main { /** * Uses getSourceForResource on the passed string and tries to open whatever resource * is passed and return the correspondent open input stream. * @return an open input stream or null. No exceptions are thrown. */ public static InputStream getInputStreamForResource(String s) { InputStream ret = null; Object o = getSourceForResource(s); if (o != null) { if (o instanceof File) { try { ret = new FileInputStream((File) o); } catch (FileNotFoundException e) { } } else if (o instanceof URL) { try { ret = ((URL) o).openStream(); } 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; } }