集册 Java实例教程 将指定的资源文件从jar文件复制到当前目录。

将指定的资源文件从jar文件复制到当前目录。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

885
将指定的资源文件从jar文件复制到当前目录。
/*
 from N o w  J a v a  .   c o m 
*/


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;
/* from 
n o w j a v a . c o m*/

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 = "nowjava.com";

        copyResourceFile(fileName);

    }

    /**

     * 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 { 
展开阅读全文