⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileoperutility.java

📁 JDesktop Integration Components (JDIC)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is * subject to license terms. *  * This program is free software; you can redistribute it and/or modify * it under the terms of the Lesser GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * General Public License for more details. *  * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package org.jdesktop.jdic.packager.impl;import java.io.File;import java.io.InputStream;import java.io.OutputStream;import java.io.DataOutputStream;import java.io.DataInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.StringTokenizer;import java.net.URL;import com.sun.deploy.net.proxy.DeployProxySelector;import com.sun.deploy.net.proxy.StaticProxyManager;import com.sun.deploy.services.ServiceManager;import com.sun.deploy.services.PlatformType;import com.sun.javaws.jnl.ExtensionDesc;import com.sun.javaws.jnl.IconDesc;import com.sun.javaws.jnl.InformationDesc;import com.sun.javaws.jnl.JARDesc;import com.sun.javaws.jnl.JREDesc;import com.sun.javaws.jnl.LaunchDesc;import com.sun.javaws.jnl.LaunchDescFactory;import com.sun.javaws.jnl.PackageDesc;import com.sun.javaws.jnl.PropertyDesc;import com.sun.javaws.jnl.ResourceVisitor;import com.sun.javaws.jnl.ResourcesDesc;/** * This class contains some Utilities related to File Operation. */public class FileOperUtility {    static {        if (System.getProperty("os.name").indexOf("Windows") != -1) {            ServiceManager.setService(PlatformType.STANDALONE_TIGER_WIN32);        } else {            ServiceManager.setService(PlatformType.STANDALONE_TIGER_UNIX);        }                try {            DeployProxySelector.reset();        } catch (Throwable t) {            StaticProxyManager.reset();        }       }        /**     * copy remote file pointed by url to a local directory      *      * @param url points to a remote file     * @param codebase remote codebase     * @param localbase local directory where store the file     * @throws IOException     */    public static void urlFile2LocalFile(URL url, URL codebase,            String localbase) throws IOException {        if (url == null || url.getFile() == null || url.getFile().length() <= 0)            return;        String relPath = getRelativePath(url.toString(), codebase.toString());        File localFile = new File(localbase + File.separator + relPath);        copyRemoteFile(url, localFile);    }        private static void createLocalFile(File localFile) throws IOException {        if (localFile == null)             return;                if (!localFile.getParentFile().exists()) {            try {                 if (!localFile.getParentFile().mkdirs()) {                     throw new IOException("Cannot make parent directory " +                           "when trying to create local file");                  }            } catch (Exception e) {                 throw new IOException("Cannot make parent directory when " +                       "trying to create local file: " + e.getMessage());              }        }               if (localFile.exists()) {            try {                if (!localFile.delete()) {                     throw new IOException("Cannot delete original file when " +                            "trying to create local file");                   }            } catch (Exception e) {                throw new IOException("Cannot delete original file when " +                        "trying to create local file: " + e.getMessage());                }        }        try {            if (!localFile.createNewFile()) {                throw new IOException("Cannot create new local file");               }        } catch (Exception e) {            throw new IOException("Cannot create new local file: " +                    e.getMessage());           }    }        private static void copyRemoteFile(URL url, File localFile)            throws IOException {        if (url == null || localFile == null)             return;                if (url.getFile() == "" || url.getFile() == null)             return;                if (localFile.isDirectory()) {            localFile = new File(localFile.getPath() + File.separator +                     url.getFile());        }                if (!localFile.exists()) {            createLocalFile(localFile);           }             DataInputStream inStream = new DataInputStream(url.openStream());        DataOutputStream outStream = new DataOutputStream(                new FileOutputStream(localFile));        copyStream(inStream, outStream);    }        private static void copyStream(InputStream inStream,     		OutputStream outStream) throws IOException {        int readbytes = 0;                                        try {            do {                byte[] buffer = new byte[512];                readbytes = inStream.read(buffer, 0, 512);                if (readbytes <= 0) {                    break;                }                                outStream.write(buffer, 0, readbytes);                outStream.flush();             } while (true);           } catch (IOException ioE) {        	ioE.printStackTrace();        } finally {            if (inStream != null) {                inStream.close();            }            if (outStream != null) {                outStream.close();            }        }    }        /**     * get relative path according the give path and base     *      * @param path      * @param base     * @return relative path     */    public static String getRelativePath(String path, String base) {        if (path == null || base == null)             return null;        // On Windows Plaform, change all the path string to lower case        if (System.getProperty("os.name").toLowerCase().startsWith("windows")) {            path = path.toLowerCase();            base = base.toLowerCase();        }        if (path.lastIndexOf(base) < 0) {            int index = path.lastIndexOf("/");        	if (index < 0) {        		return path;        	} else {        		return path.substring(index + 1);        	}        }        String relPath = path.substring(path.lastIndexOf(base) + base.length());        StringTokenizer st = new StringTokenizer(relPath, "/", false);        String nativeRelPath = "";        while (st.hasMoreTokens()) {            if (nativeRelPath.length() == 0) {                nativeRelPath = st.nextToken();            } else {                nativeRelPath += File.separator + st.nextToken();            }        }                return nativeRelPath;    }        /**     * copy source file to dest     *      * @param sourceFileName name of the source, could be either directory or a file     * @param destFileName name of the dest, could be either a file or a directory     * @throws IOException     */    public static void copyLocalFile(String sourceFileName, String destFileName)            throws IOException {        File sourceFile = new File(sourceFileName);        File destFile = new File(destFileName);                if (!sourceFile.exists())             return;                /* return if source and dest are pointing at the same path */        if (destFile.exists() && sourceFile.getPath().        		equals(destFile.getPath())) {        	return;        }                if (sourceFile.isFile()) {             if (destFile.isDirectory()) {                destFile = new File(destFile.getPath() + File.separator +                        sourceFile.getName());               }            createLocalFile(destFile);            DataInputStream inStream = new DataInputStream(                    new FileInputStream(sourceFile));            DataOutputStream outStream = new DataOutputStream(                    new FileOutputStream(destFile));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -