java 复制文件

java 进行文件复制,代码如下

/**
  * sourceFile 为被复制的文件, destinationFile 为目标文件
  * @param sourceFile
  * @param destinationFile
  * @throws IOException
  */
public static void copyFile(File sourceFile, File destinationFile) throws IOException {
		
    InputStream input = null;
    OutputStream output = null;
		
    try {
			
        input = new FileInputStream(sourceFile);  //从sourceFile文件中获得输入字节
        output = new FileOutputStream(destinationFile);
			
        byte[] buf = new byte[1024];
        int bytesRead; 
			
        while((bytesRead = input.read(buf)) > 0) {
             output.write(buf, 0, bytesRead);
        }
			
        } catch (FileNotFoundException e) {
             e.printStackTrace();
        }finally {
             input.close();
             output.close();
        }	
}

测试

package nice;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class DemoL {

	public static void main(String[] args) throws IOException {

		//File file = new File("E:\\谷歌下载\\20180318.txt");
		
		//File file = new File("E:\\谷歌下载\\作曲系初试科目选考表.xls");
		
		//File file = new File("E:\\谷歌下载\\timg123.jpg");
		
		File file = new File("E:\\谷歌下载\\命运还是巧合.mp3");
		
		
		File dest = new File("C:\\Users\\Administrator\\Desktop\\file\\a.mp3");
		
		if(!dest.exists()) {
			//dest.mkdirs();  //创建文件夹
			dest.createNewFile();  // 创建文件
			System.out.println("创建成功");
		}
		
		
		try {
			copyFile(file, dest);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * sourceFile 为被复制的文件, destinationFile 为目标文件
	 * @param sourceFile
	 * @param destinationFile
	 * @throws IOException
	 */
	public static void copyFile(File sourceFile, File destinationFile) throws IOException {
		
		InputStream input = null;
		OutputStream output = null;
		
		try {
			
			input = new FileInputStream(sourceFile);  //从sourceFile文件中获得输入字节
			output = new FileOutputStream(destinationFile);
			
			byte[] buf = new byte[1024];
			int bytesRead; 
			
			while((bytesRead = input.read(buf)) > 0) {
				output.write(buf, 0, bytesRead);
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally {
			input.close();
			output.close();
		}
		
	}
}

 

除此之外,还有其他封装好的方法可以直接调用

使用 Java NIO包下的 transferFrom 方法,进行复制

package nice.com.priactice;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class DemoO {

	public static void main(String[] args) throws IOException {
		
		File source = new File("E:\\谷歌下载\\答案_伍佰.mp3");
		File dest = new File("C:\\Users\\Administrator\\Desktop\\file\\答案.mp3");
		
		copyFileUseFileChannels(source, dest);
	}
	
	/**
	 * source 为被复制的文件, dest 为目标文件
	 * @param source
	 * @param dest
	 * @throws IOException
	 */
	public static void copyFileUseFileChannels(File source, File dest) throws IOException {
		FileChannel inputChannel = null;    
		FileChannel outputChannel = null;  
		
		try {
			
			inputChannel = new FileInputStream(source).getChannel();
			outputChannel = new FileOutputStream(dest).getChannel();
			
			outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally {
			inputChannel.close();
			outputChannel.close();
		}
	}
}

 

使用 Apache Commons IO 包提供的复制文件的方法

首先在项目中引入 commons-io jar 包

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

commons-io 使用代码如下

package com.demo;

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class DemoFileCopy {

	public static void main(String[] args) throws IOException {
		
		File srcFile = new File("E:\\谷歌下载\\真的爱你独唱.mp3");
		File destFile = new File("C:\\Users\\Administrator\\Desktop\\file\\真的爱你独唱.mp3");
		
		if(! destFile.exists()) {
			destFile.createNewFile();
		}

		FileUtils.copyFile(srcFile, destFile);
	}
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

悟世君子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值