Here you can find the source of copyResource(Class> cls, String strResSource, String strFile)
Parameter | Description |
---|---|
cls | Class with valid priviledge |
strResSource | resource path |
strFile | file to copy to |
// ////////////////////////////////////////////////////// public static boolean copyResource(Class<?> cls, String strResSource, String strFile)
//package com.java2s; import java.io.*; public class Main { public static final int BUFFER_SIZE = 65536; /**//from ww w . j a v a2 s .c o m * Copy resource to file * * @param cls * Class with valid priviledge * @param strResSource * resource path * @param strFile * file to copy to * @return true if succees, otherswise false * @author Thai Hoang Hiep */ // ////////////////////////////////////////////////////// public static boolean copyResource(Class<?> cls, String strResSource, String strFile) { InputStream isSrc = null; FileOutputStream osDest = null; try { isSrc = cls.getResourceAsStream(strResSource); if (isSrc == null) throw new IOException("Resource " + strResSource + " not found"); osDest = new FileOutputStream(strFile); byte btData[] = new byte[BUFFER_SIZE]; int iLength; while ((iLength = isSrc.read(btData)) != -1) osDest.write(btData, 0, iLength); } catch (IOException e) { e.printStackTrace(); return false; } finally { safeClose(isSrc); safeClose(osDest); } return true; } /** * Close object safely * * @param is * InputStream */ // ////////////////////////////////////////////////////// public static void safeClose(InputStream is) { try { if (is != null) is.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Close object safely * * @param os * OutputStream */ // ////////////////////////////////////////////////////// public static void safeClose(OutputStream os) { try { if (os != null) os.close(); } catch (Exception e) { e.printStackTrace(); } } /** * Close object safely * * @param fl * RandomAccessFile */ // ////////////////////////////////////////////////////// public static void safeClose(RandomAccessFile fl) { try { if (fl != null) fl.close(); } catch (Exception e) { e.printStackTrace(); } } }