Mega Code Archive

 
Categories / Java / File Input Output
 

Retreive Binary File From Jar

/*  * Copyright 2006 - 2008 Georges Stephan  *   * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *   *      http://www.apache.org/licenses/LICENSE-2.0  *   * Unless required by applicable law or agreed to in writing, software  * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  * See the License for the specific language governing permissions and  * limitations under the License.  */ import java.util.logging.*; import java.io.*; /**  *   * @author Georges Stephan  */ public class Util {   private static Logger logger = Logger.getLogger(Util.class.getName());        public static String validateString(String aString) {     if(aString==null) return("");          aString=aString.replace('\'', ' ');     aString=aString.trim();     return(aString);   }        public static String removeNonDigits(String aString) {     StringBuffer newString = new StringBuffer();     for(int x=0;x<aString.length();x++) {       if(Character.isDigit(aString.charAt(x))) newString.append(aString.charAt(x));     }     return(newString.toString());   }        public static void createHome() throws Exception {     File ch = new File(System.getProperty("user.home")+File.separator+".dataform");     if(!ch.exists()) {       if(!ch.mkdir()) throw new Exception("Failed to create the dataform configuration directory");     }   }        public static String getHomeDir() {     return(System.getProperty("user.home")+File.separator+".dataform"+File.separator);   }         public static boolean retreiveTextFileFromJar(String resourceName,String targetDirectory) throws Exception {     boolean found=false;     if(resourceName!=null) {       InputStream is = Util.class.getResourceAsStream(resourceName);       if(is==null) logger.log(Level.WARNING,"The resource '"+resourceName+"' was not found.");       BufferedReader br = new BufferedReader(new InputStreamReader(is));       String line;       String lineSep = System.getProperty("line.separator");       StringBuffer sb = new StringBuffer();       while ((line = br.readLine()) != null) {         sb.append(line);         sb.append(lineSep);       }       is.close();       if(sb!=null) {         if(sb.length()>0) {           FileWriter temp = new FileWriter(targetDirectory+File.separator+resourceName);           temp.write(sb.toString());           temp.close();           found=true;         }       }     }     return(found);       }      public static boolean retreiveBinaryFileFromJar(String resourceName,String targetDirectory,Object resource) throws Exception {     boolean found=false;     if(resourceName!=null) {       InputStream is = resource.getClass().getResourceAsStream(resourceName);       if(is==null) throw new Exception ("Resource "+resourceName+" was not found.");       BufferedReader br = new BufferedReader(new InputStreamReader(is));       FileOutputStream fos = new FileOutputStream(targetDirectory+File.separator+resourceName.substring(resourceName.lastIndexOf('/'),resourceName.length()));       byte[] buffer = new byte[1024];             int bytesRead;                          while ((bytesRead = is.read(buffer)) != -1) {                 fos.write(buffer, 0, bytesRead);             }             fos.flush();             br.close();             is.close();       found=true;     } else {       found=false;     }     return found;   } }