Android File Copy copyResource(Class cls, String strResSource, String strFile)

Here you can find the source of copyResource(Class cls, String strResSource, String strFile)

Description

Copy resource to file

Parameter

Parameter Description
cls Class with valid priviledge
strResSource resource path
strFile file to copy to

Return

true if succees, otherswise false

Declaration

// //////////////////////////////////////////////////////
public static boolean copyResource(Class<?> cls, String strResSource,
        String strFile) 

Method Source Code

//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();
        }
    }
}

Related

  1. copyFileByCommand(String src, String dst, boolean isMove)
  2. copyFileLazy(String source, String destination)
  3. copyFileToFile(File file, File outFile)
  4. copyFiles(File src, File dst)
  5. copyImgFile(String sourcePath, String fileId)
  6. copyStringToFile(String content, String FilePath)
  7. copyfile(File source, File destination)
  8. dumbfilecopy(File source, File dest)
  9. fileCopy(File dbFile, File backup)