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

📄 fileoperation.java

📁 一个数据挖掘软件ALPHAMINERR的整个过程的JAVA版源代码
💻 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.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;

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;
	}
}

⌨️ 快捷键说明

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