方法一
try {
File file1 = new File("D:/aa/a.docx");//准备好的文件(D盘下的aa文件夹中的a.docx文件)
File file2 = new File("D:/bb/"+file1.getName());//复制到的位置
FileUtils.copyFile(file1, file2);//复制文件
} catch (IOException e) {
e.printStackTrace();
}
方法二
try {
File file1 = new File("D:/aa/a.docx");//准备好的文件
File file2 = new File("D:/bb/"+file1.getName());//复制到的位置
FileChannel inputChannel = new FileInputStream(file1).getChannel();
FileChannel outputChannel = new FileOutputStream(file2).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
inputChannel.close();
outputChannel.close();
} catch (IOException e) {
e.printStackTrace();
}
方法三
try {
File file1 = new File("D:/aa/a.docx");//准备好的文件(D盘下的aa文件夹中的a.docx文件)
File file2 = new File("D:/bb/"+file1.getName());//复制到的位置
InputStream input = new FileInputStream(file1);//输入流
OutputStream output = new FileOutputStream(file2);//输出流
byte[] buf = new byte[1024];
int bytesRead;
//开始复制
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
input.close();//关闭输入流
output.close();//关闭输出流
} catch (IOException e) {
e.printStackTrace();
}