All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.ucm.fdi.gaia.jcolibri.util.FileIO Maven / Gradle / Ivy

Go to download

jCOLIBRI is a java framework for the development of Case-Based Reasoning systems.

There is a newer version: 3.2
Show newest version
/**
 * FileIO.java
 * jCOLIBRI2 framework. 
 * @author Juan A. Recio-Garc�a.
 * GAIA - Group for Artificial Intelligence Applications
 * http://gaia.fdi.ucm.es
 * 03/01/2007
 */
package es.ucm.fdi.gaia.jcolibri.util;

import org.apache.logging.log4j.LogManager;

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

/**
 * Utility functions to transparently access to files in the file system, jar file, classpath...
 * @author Juan A. Recio-Garcia
 * @version 1.0
 */
public class FileIO {

	/**
	 * Returns the URL that localizes a file. It tries the following locations:
	 * 
    *
  • The path recieved by parameter *
  • "bin/"+path *
  • "src/"+path *
  • "/"+path *
  • path inside a jar *
  • "/"+path inside a jar *
*/ @SuppressWarnings("deprecation") public static URL findFile(String file) { File f; try { f = new File(file); if(f.exists()) return f.toURL(); } catch (MalformedURLException e1) {} try { f = new File("bin/"+file); if(f.exists()) return f.toURL(); } catch (MalformedURLException e1) {} try { f = new File("src/"+file); if(f.exists()) return f.toURL(); } catch (MalformedURLException e1) {} file = file.replace('\\','/'); try { URL url = FileIO.class.getResource(file); if(url!= null) return url; } catch (Exception e) {} try { URL url = FileIO.class.getResource("/"+file); if(url!= null) return url; } catch (Exception e) {} LogManager.getLogger(FileIO.class).warn("File not found: "+file); return null; } /** * Tries to return an input stream of the file */ public static InputStream openFile(String file) { URL url = null; try { url = new URL(file); return url.openStream(); }catch(Exception e) {} url = findFile(file); try { return url.openStream(); }catch(Exception e) {} file = file.replace('\\','/'); file = file.substring(file.indexOf('!')+1); try { return FileIO.class.getResourceAsStream(file); } catch (Exception e) {} try { return FileIO.class.getResourceAsStream("/"+file); } catch (Exception e) {} LogManager.getLogger(FileIO.class).warn("Error opening stream for: "+file); return null; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy