View Javadoc

1   package be.dvw.administration.util;
2   
3   import java.io.*;
4   import java.net.URL;
5   
6   /***
7    * This class allows to safely load files from anywhere in the classpath
8    */
9   public class FileUtils
10  {
11  	public static InputStream getFileInputStream(String filePath) throws IOException
12  	{
13  		InputStream is =  new FileInputStream (getFile(filePath));
14  		if (is == null)
15  		{
16  			throw new IOException("Unable to read file " + filePath);
17  		}
18  		return is;
19  	}
20  
21  	public static InputStreamReader getInputStreamReader(String filePath) throws IOException
22  	{
23  		InputStream is = getFileInputStream(filePath);
24  		return new InputStreamReader(is);
25  
26  	}
27  
28  	public static File getFile(String filePath)
29  	{
30  		URL url = getUrl(filePath);
31  		return new File ( url.getFile() );
32  	}
33  
34  	public static URL getUrl(String filePath)
35  	{
36  		ClassLoader cl = Thread.currentThread().getContextClassLoader();
37  		return cl.getResource( filePath );
38  	}
39  
40  }