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