1 18 19 package org.objectweb.jac.util; 20 21 import java.io.*; 22 import java.net.*; 23 24 29 public class URLInputStream { 30 31 private InputStream inputStream; 32 private URL url; 33 34 public URLInputStream(String fileLocation) throws Exception { 35 36 try { 37 inputStream = new FileInputStream(fileLocation); 38 url=new URL("file:"+fileLocation); 39 } catch ( FileNotFoundException e ) { 41 try { 42 url = getClass().getClassLoader().getResource( fileLocation ); 43 inputStream = url.openStream(); 44 } catch (Exception e2 ) { 45 url = new URL(fileLocation); 47 inputStream = url.openStream(); 48 } 49 } 50 } 51 52 public URL getURL() { 53 return url; 54 } 55 56 public InputStream getInputStream() { 57 return inputStream; 58 } 59 60 public static void main(String [] args) throws Exception { 61 if ( args.length < 1 ) { 62 System.err.println("usage: java InputStreamTest <file_path>"); 63 } else { 64 for ( int i=0; i<args.length; i++) { 65 try { 66 URLInputStream urlInputStream = new URLInputStream( args[i] ); 67 System.out.println( urlInputStream.getURL().toExternalForm()); 68 urlInputStream.getInputStream().close(); 69 } catch( Exception e ) { 70 System.out.println(e.getMessage()); 71 } 72 System.out.println(); 73 } 74 } 75 76 System.exit(0); 77 } 78 79 } 80 | Popular Tags |