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

📄 fileoperation.java

📁 为了下东西 随便发了个 datamining 的源代码
💻 JAVA
字号:
/*
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*
 * Created on 2006/09/02
 *
 * @Author: Xiaojun Chen
 * $Revision$ 1.0
 *
 */
package eti.bi.common.System;
import java.io.ByteArrayInputStream;
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.net.URL;
import java.util.Iterator;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class FileOperation {
	public static boolean copyFile(File file, File newFile) throws Exception {
		if (newFile.exists()) {
			throw new IOException("The file: " + newFile.getAbsolutePath() + " already exists!");
		}
		InputStream in = null;
		OutputStream ou = null;
		try {
			in = new FileInputStream(file);
			ou = new FileOutputStream(newFile);
			byte[] bytes = new byte[5000];
			int read = 0;
			int size = in.available();
			int hasread = 0;
			int step = 0;
			while (hasread < size) {
				read = 0;
				step = 5000 > (size - hasread) ? (size - hasread) : 5000;
				while (read < step) {
					read += in.read(bytes, read, step - read);
				}
				hasread += read;
				ou.write(bytes, 0, read);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (ou != null) {
					ou.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	public static boolean copyInputStream(InputStream in, File newFile) throws Exception {
		if (newFile.exists()) {
			throw new IOException("The file: " + newFile.getAbsolutePath() + " already exists!");
		}
		OutputStream ou = new FileOutputStream(newFile);
		try {
			ou = new FileOutputStream(newFile);
			byte[] bytes = new byte[5000];
			int read = 0;
			int size = in.available();
			int hasread = 0;
			int step = 0;
			while (hasread < size) {
				read = 0;
				step = 5000 > (size - hasread) ? (size - hasread) : 5000;
				while (read < step) {
					read += in.read(bytes, read, step - read);
				}
				hasread += read;
				ou.write(bytes, 0, read);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (ou != null) {
					ou.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	public static boolean copyURL(URL url, File newFile) throws Exception {
		if (newFile.exists()) {
			throw new IOException("The file: " + newFile.getAbsolutePath() + " already exists!");
		}
		InputStream in = null;
		OutputStream ou = new FileOutputStream(newFile);
		try {
			in = url.openStream();
			ou = new FileOutputStream(newFile);
			byte[] bytes = new byte[5000];
			int read = 0;
			int size = in.available();
			int hasread = 0;
			int step = 0;
			while (hasread < size) {
				read = 0;
				step = 5000 > (size - hasread) ? (size - hasread) : 5000;
				while (read < step) {
					read += in.read(bytes, read, step - read);
				}
				hasread += read;
				ou.write(bytes, 0, read);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				if (in != null) {
					in.close();
				}
				if (ou != null) {
					ou.close();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	public static Manifest CreateManifest(Attributes mainManifest, Map<String, Attributes> entries) throws Exception{
		
		//Create a manifest from a file
		File tempFile = new File(SystemTempFile.getCaseTempHome()+"manifestfile");
		tempFile.createNewFile();
		InputStream fis = new FileInputStream(tempFile);
        Manifest manifest = new Manifest(fis);
    
        // Construct a string version of a manifest
        StringBuffer sbuf = new StringBuffer();
        sbuf.append("Manifest-Version: 1.0\n");
        
        if(mainManifest!=null) {
        	Iterator keys = mainManifest.keySet().iterator();
        	while(keys.hasNext()) {
        		Object key = keys.next();
        		sbuf.append(key+": "+mainManifest.get(key)+"\n");
        	}
        }
        
        sbuf.append("\n");
        
        if(entries!=null) {
        	Iterator<String> entrieKeys = entries.keySet().iterator();
        	while(entrieKeys.hasNext()) {
        		Attributes attr = entries.get(entrieKeys.next());
        		
        		Iterator keys = attr.keySet().iterator();
            	while(keys.hasNext()) {
            		Object key = keys.next();
            		sbuf.append(key+": "+mainManifest.get(key)+"\n");
            	}
        	}
        }
        
        // Convert the string to a input stream
        InputStream is = new ByteArrayInputStream(sbuf.toString().getBytes("UTF-8"));
    
        // Create the manifest
        manifest = new Manifest(is);

        return manifest;
	}
	
	public static boolean Jar(File newFile, File[] files, Attributes mainManifest, Map<String, Attributes> entries) throws Exception{
		byte b[] = new byte[512];
			
		Manifest man = CreateManifest(mainManifest, entries);
		
		JarOutputStream jout = new JarOutputStream(new FileOutputStream(newFile), man);
		
		for (int i = 0; i < files.length; i++) {
			InputStream in = new FileInputStream(files[i]);
			ZipEntry e = new ZipEntry(files[i].getName().replace(File.separatorChar, '/'));
			jout.putNextEntry(e);
			int len = 0;
			while ((len = in.read(b)) != -1) {
				jout.write(b, 0, len);
			}
			jout.closeEntry();
		}
		jout.close();
		
		return true;
	}
	
	public static boolean Zip(File newFile, File[] files) throws Exception{
		byte b[] = new byte[512];
		ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(newFile));
		for (int i = 0; i < files.length; i++) {
			InputStream in = new FileInputStream(files[i]);
			ZipEntry e = new ZipEntry(files[i].getName().replace(File.separatorChar, '/'));
			zout.putNextEntry(e);
			int len = 0;
			while ((len = in.read(b)) != -1) {
				zout.write(b, 0, len);
			}
			zout.closeEntry();
		}
		zout.close();
		
		return true;
	}
	
}

⌨️ 快捷键说明

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