利用Java语言,根据文件的路径,实现文件复制
package com.javabasic.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* @Description java实现复制文件
* @ClassName FileUtils
* @Author yuhuofei
* @Date 2022/3/6 20:55
* @Version 1.0
*/
public class FileUtils {
/**
* 根据文件路径实现文件复制
*
* @param sourceFilePath
* @param targetFilePath
* @throws IOException
*/
public static void copyFile(String sourceFilePath, String targetFilePath) throws IOException {
File sourceFile = new File(sourceFilePath);
File targetFile = new File(targetFilePath);
FileInputStream inputStream = new FileInputStream(sourceFile);
FileOutputStream outputStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[4096];
int length = 0;
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
//测试方法
public static void main(String[] args) throws IOException {
String sourcePath = "D:/Test/demo/aaa.txt";
String targetPath = "D:/Test/copy_aaa.txt";
copyFile(sourcePath, targetPath);
}
}