Copies the specified resource file into the current directory from the jar file. - Java Reflection

Java examples for Reflection:Jar

Description

Copies the specified resource file into the current directory from the jar file.

Demo Code


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;

public class Main{
    public static void main(String[] argv) throws Exception{
        String fileName = "java2s.com";
        copyResourceFile(fileName);// ww  w. j  a  va 2 s .c  o m
    }
    /**
     * Copies the specified resource file into the current directory from
     * the jar file. If the file already exists, no copy is performed.
     *
     * @param fileName the name of the file to copy, relative to the jar 
     *  file -- such as "com/limegroup/gnutella/gui/images/image.gif"
     */
    public static void copyResourceFile(final String fileName) {
        copyResourceFile(fileName, null);
    }
    /**
     * Copies the specified resource file into the current directory from
     * the jar file. If the file already exists, no copy is performed.
     *
     * @param fileName the name of the file to copy, relative to the jar
     *  file -- such as "com/limegroup/gnutella/gui/images/image.gif"
     * @param newFile the new <tt>File</tt> instance where the resource file
     *  will be copied to
     */
    public static void copyResourceFile(final String fileName, File newFile) {
        copyResourceFile(fileName, newFile, false);
    }
    /**
     * Copies the specified resource file into the current directory from
     * the jar file. If the file already exists, no copy is performed.
     *
     * @param fileName the name of the file to copy, relative to the jar 
     *  file -- such as "com/limegroup/gnutella/gui/images/image.gif"
     * @param newFile the new <tt>File</tt> instance where the resource file
     *  will be copied to -- if this argument is null, the file will be
     *  copied to the current directory
     * @param forceOverwrite specifies whether or not to overwrite the 
     *  file if it already exists
     */
    public static void copyResourceFile(final String fileName,
            File newFile, final boolean forceOverwrite) {
        if (newFile == null)
            newFile = new File(".", fileName);

        // return quickly if the file is already there, no copy necessary
        if (!forceOverwrite && newFile.exists())
            return;
        String parentString = newFile.getParent();
        if (parentString == null) {
            return;
        }
        File parentFile = new File(parentString);
        if (!parentFile.isDirectory()) {
            parentFile.mkdirs();
        }

        ClassLoader cl = CommonUtils.class.getClassLoader();

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            //load resource using my class loader or system class loader
            //Can happen if Launcher loaded by system class loader
            URL resource = cl != null ? cl.getResource(fileName)
                    : ClassLoader.getSystemResource(fileName);

            if (resource == null)
                throw new NullPointerException("resource: " + fileName
                        + " doesn't exist.");

            InputStream is = resource.openStream();

            //buffer the streams to improve I/O performance
            final int bufferSize = 2048;
            bis = new BufferedInputStream(is, bufferSize);
            bos = new BufferedOutputStream(new FileOutputStream(newFile),
                    bufferSize);
            byte[] buffer = new byte[bufferSize];
            int c = 0;

            do { //read and write in chunks of buffer size until EOF reached
                c = bis.read(buffer, 0, bufferSize);
                if (c > 0)
                    bos.write(buffer, 0, c);
            } while (c == bufferSize); //(# of bytes read)c will = bufferSize until EOF

        } catch (IOException e) {
            //if there is any error, delete any portion of file that did write
            newFile.delete();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException ignored) {
                }
            }
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException ignored) {
                }
            }
        }
    }
}

Related Tutorials