使用FileChannel对文件进行拷贝

public static void fcCopy(String srcFile,String destFile) throws IOException{        //传入参数为文件的复制路径和粘贴路径
		File copy = null;
		File paste = null;
		FileInputStream fileInputStream = null;
		FileChannel fileChannel = null;
		FileOutputStream fileOutputStream = null;
		FileChannel fileChannel2 = null;	
		try{	
		    copy = new File(srcFile);  
		    paste = new File(destFile);
		    fileInputStream = new FileInputStream(copy);
	            fileChannel = fileInputStream.getChannel(); 
		    paste.createNewFile();
		    fileOutputStream = new FileOutputStream(paste);
		    fileChannel2 = fileOutputStream.getChannel();
	            long fileSize = copy.length();  
	            int writeLength = 0;  
	            while (true) {     
	                writeLength += fileChannel2.transferFrom(fileChannel, writeLength, fileSize - writeLength);  
	            	if (writeLength == fileSize) {  
	            	    break;  
	            	}  
	            }
		}catch (IOException e) {
	            e.printStackTrace();
	            throw e;
	        } finally {  
	            try {  
	                if (fileChannel2!= null)  
	                	fileChannel2.close();  
	                if (fileChannel!= null)  
	                	fileChannel.close();  
	            } catch (IOException e) {  
	                e.printStackTrace();
	            }  
	        }    
	        
}

阅读更多

没有更多推荐了,返回首页